bash 脚本将在 5 分钟内运行

发布于 2024-09-28 15:51:19 字数 135 浏览 3 评论 0原文

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

我有命令:

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

但脚本每次都会立即运行。

I just want a bash script to run 5 minutes after it's called. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

生活了然无味 2024-10-05 15:51:19

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

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

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

echo /path/to/my/script | at now + 5 min
烟花肆意 2024-10-05 15:51:19

怎么样:

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

how about:

sleep 300 && /path/to/my/script
怪我鬧 2024-10-05 15:51:19
at -f /path/to/my/script -t now +5 minutes

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

无论如何祝你好运

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 ).

Good luck anyways

笔芯 2024-10-05 15:51:19

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

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

红墙和绿瓦 2024-10-05 15:51:19

命令是从左到右评估的,因此首先执行您的脚本,其输出将通过管道传输到 at 命令,这是正常行为。有关详细信息,请参阅 at 手册页

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 pages for more information.

甜味超标? 2024-10-05 15:51:19

试试这个

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
}

倒计时功能

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 "        "
}

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 "        "
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文