bash 脚本在 5 分钟内运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3964254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:46:59  来源:igfitidea点击:

bash script to run in 5 minutes

bashtimed

提问by James Nine

I just want a bash script to run 5 minutes after it's called. What am I doing wrong?

我只想让 bash 脚本在调用后运行 5 分钟。我究竟做错了什么?

I have the command:

我有命令:

/path/to/my/script | at now + 5 min

And yet the script runs right away every time.

然而,脚本每次都会立即运行。

回答by Hyman Kelly

You are executing the script immediately and sending its output into at. You need to send the name of the script itself into at:

您正在立即执行脚本并将其输出发送到at. 您需要将脚本本身的名称发送到at

echo /path/to/my/script | at now + 5 min

回答by Amir Afghani

how about:

怎么样:

sleep 300 && /path/to/my/script

回答by oddenodin

at -f /path/to/my/script -t now +5 minutes

This should work as far as scheduling a script to run at a specific time. For any more information on the "at" command try linuxmanpages.com. I may be wrong thought ( currently not at a linux system to test ).

这应该可以安排脚本在特定时间运行。有关“ at”命令的更多信息,请访问linuxmanpages.com。我的想法可能是错误的(目前不是在 linux 系统上进行测试)。

Good luck anyways

还是祝你好运

回答by Femaref

Commands are evaluated left to right, so first your script gets executed, the output of it will be piped to the at command, this is normal behaviour. Have look at at the at man pagesfor more information.

命令从左到右计算,所以首先你的脚本被执行,它的输出将通过管道传输到 at 命令,这是正常行为。查看 at手册页了解更多信息。

回答by Andrew Cooper

The problem is you're running the script and piping the output to the atcommand. What you need to do is run the atcommand with the path to your script as a parameter. I'm not sure of the syntax, but at -hor man atshould help.

问题是您正在运行脚本并将输出通过管道传输到at命令。您需要做的是at使用脚本的路径作为参数运行命令。我不知道语法的,但at -h还是man at应该有所帮助。

回答by edmondscommerce

try this

尝试这个

sys.scheduled_run /path/to/my/script 5

sys.scheduled_run /path/to/my/script 5

main function

主功能

function sys.scheduled_run(){
    local PATH_TO_ACTION MINS SLEEPTIME
    PATH_TO_ACTION=
    MINS=
    SLEEPTIME=$(($MINS * 60))
    echo "Sleeping for $MINS minute ($SLEEPTIME seconds) and then running $PATH_TO_ACTION"
    ui.countdown $SLEEPTIME
    $PATH_TO_ACTION
    echo "Done"
    if [ "REPEAT" == "" ] 
    then
        echo "Going for Repeat"
        sys.scheduled_run "$@"
    fi
}

countdown function

倒计时功能

function ui.countdown(){ #USAGE ui.countdown 60 countdown for 60 seconds
        local SECONDS=
        local START=$(date +%s)
        local END=$((START + SECONDS))
        local CUR=$START
        while [[ $CUR -lt $END ]]
        do
                CUR=$(date +%s)
                LEFT=$((END-CUR))

                printf "\r%02d:%02d:%02d" \
                        $((LEFT/3600)) $(( (LEFT/60)%60)) $((LEFT%60))

                sleep 1
        done
        echo "        "
}