Is it possible to set a cron job or bash script to run X minutes from now?

寵の児 提交于 2021-02-04 20:50:22

问题


I understand I can set a cron job to run every 5 minutes with crontab -e by adding a line such as: */5 * * * * /path/to/script.sh.

Is it possible to get the system time in minutes using date +"%M" for example, and then set a cron job to run at date +"%M" plus 5 minutes?

I know I can get date +"%M" + 5 via the following process:

$ MIN=`date +"%M"`
$ export MIN
$ expr $MIN + 5

Is it possible to use this to set a cron job or script to run at "current time in minutes" plus "X minutes"?

I could imagine this being useful in an application in which a user creates a new document and then is prompted to save or title the document X minutes after creating it.


回答1:


You should use the at command instead.

With at, you can specify the time when a command should be run using time or even keywords like midnight, teatime, tomorrow etc..

You can specify the time after 5 min like this:

at now + 5 min

And then enter the command you want to schedule. Or you can enter your scheduled jobs in a jobs file and give it as a argument for the at command using the -f option.

Sample of a jobs file:

$ cat myjobs.txt
/path/to/a/shell-script.sh
/path/to/any/command/or/script.sh

The following command will execute those jobs after 5 mins:

$ at -f myjobs.txt now + 5 min

Check this link for more information.




回答2:


Look at the at command. Maybe this is what you are looking for.

See

  • http://www.computerhope.com/unix/uat.htm
  • http://www.thegeekstuff.com/2010/06/at-atq-atrm-batch-command-examples


来源:https://stackoverflow.com/questions/34746267/is-it-possible-to-set-a-cron-job-or-bash-script-to-run-x-minutes-from-now

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