Run a command at a specific time

限于喜欢 提交于 2019-12-20 08:59:46

问题


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

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?

Any suggestions most welcome.

Thanks in advance,


回答1:


You could try this:

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

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

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



回答2:


You can echo your command into at as input:

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



回答3:


In bash or zsh you can say

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 cron for regularly scheduled jobs; the crontab entry would look like

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




回答4:


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




回答5:


The at command 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 at functionality as follows:

#! /bin/bash

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

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

        split($4, tmp, ":" );

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

    }' )

done

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



来源:https://stackoverflow.com/questions/5734365/run-a-command-at-a-specific-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!