Linux 在特定时间运行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5734365/
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-08-05 03:45:02  来源:igfitidea点击:

Run a command at a specific time

linuxbashcommandat-job

提问by Eamorr

I'm trying to run a command at a specific time. I've looked at the "at" command, but I don't know how to get it working...

我正在尝试在特定时间运行命令。我看过“at”命令,但我不知道如何让它工作......

Here's what I do:

这是我所做的:

at 1843 (Enter)
php /run/this/script.php (Ctrl+D)

But how do I do this in a bash script? I mean, I need to press enter and "Ctrl+D" to set up the delay... How to do this in a script?

但是我如何在 bash 脚本中做到这一点?我的意思是,我需要按 Enter 和“Ctrl+D”来设置延迟...如何在脚本中执行此操作?

Any suggestions most welcome.

任何建议最欢迎。

Thanks in advance,

提前致谢,

采纳答案by Pointy

You could try this:

你可以试试这个:

at 1843 <<_EOF_
php /run/this/script.php
_EOF_

editif what you want to do is run Firefox, try this:

编辑如果你想做的是运行 Firefox,试试这个:

at 1843 <<_EOF_
DISPLAY=:0.0 /usr/bin/firefox
_EOF_

回答by geekosaur

In bashor zshyou can say

bash或者zsh你可以说

at 1843 <<< 'php /run/this/script.php'

Failing that, you need to use a here document:

否则,您需要使用此处的文档:

at 1843 <<EOF
php /run/this/script.php
EOF

You might also want to look into cronfor regularly scheduled jobs; the crontabentry would look like

您可能还想查看cron定期安排的工作;该crontab条目将看起来像

43 18 * * * php /run/this/script.php

(EDIT: whoops, helps to recall which version of at. I think that may have been a local mod.)

(编辑:哎呀,有助于回忆哪个版本的at。我认为这可能是本地 mod。)

回答by drysdam

echo "php /run/this/script.php" | at 18:43

echo "php /run/this/script.php" | at 18:43

回答by Michael Berkowski

You can echo your command into atas input:

您可以将您的命令at作为输入回显:

echo "/usr/bin/php /run/this/script.php" | at 18:43

回答by funk

The atcommand is not installed by default to the systems I work (Ubuntu, RHEL) and I don't have permissions to install new software, so I use scripts that combine bash and awk in order to achieve the atfunctionality as follows:

命令不被默认为系统的I工作(Ubuntu的,RHEL)安装和我没有权限来安装新的软件,所以我用,为了实现结合bash和的awk脚本功能如下:

#! /bin/bash

res=""
while [ ! $res ]; do

    res=$( date | awk -v hour= -v min= '{ 

        split(, tmp, ":" );

        if( (tmp[1] == hour) && (tmp[2] == min) )
        {
            print "ok";   
        }
        else
        {
            print "";
        }

    }' )

done

./atReplacement.sh $HOUR $MIN; [OTHER_COMMANDS]

./atReplacement.sh $HOUR $MIN; [OTHER_COMMANDS]