简体   繁体   English

bash脚本在5分钟内运行

[英]bash script to run in 5 minutes

I just want a bash script to run 5 minutes after it's called. 我只想要一个bash脚本在调用后5分钟运行。 What am I doing wrong? 我究竟做错了什么?

I have the command: 我有命令:

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

And yet the script runs right away every time. 然而,剧本每次都会立即运行。

You are executing the script immediately and sending its output into at . 您立即执行脚本,将其输出发送到at You need to send the name of the script itself into at : 您需要将脚本本身的名称发送到at

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

怎么样:

sleep 300 && /path/to/my/script
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 . 有关“ at ”命令的更多信息,请尝试linuxmanpages.com I may be wrong thought ( currently not at a linux system to test ). 我可能是错的想法(目前还没有在linux系统上测试)。

Good luck anyways 祝你好运

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. 命令从左到右进行计算,因此首先执行脚本,将其输出通过管道传递给at命令,这是正常行为。 Have look at at the at man pages for more information. 有关更多信息,请查看手册页

The problem is you're running the script and piping the output to the at command. 问题是您正在运行脚本并将输出传递给at命令。 What you need to do is run the at command with the path to your script as a parameter. 您需要做的是运行at命令,并将脚本路径作为参数。 I'm not sure of the syntax, but at -h or man at should help. 我不确定语法,但at -hman at应该有帮助。

try this 试试这个

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

main function 主功能

function sys.scheduled_run(){
    local PATH_TO_ACTION MINS SLEEPTIME
    PATH_TO_ACTION=$1
    MINS=$2
    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" == "$3" ] 
    then
        echo "Going for Repeat"
        sys.scheduled_run "$@"
    fi
}

countdown function 倒计时功能

function ui.countdown(){ #USAGE ui.countdown 60 countdown for 60 seconds
        local SECONDS=$1
        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 "        "
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM