问题描述
我知道我可以使用 crontab -e
将 cron 作业设置为每 5 分钟运行一次,方法是添加如下一行:*/5 * * * */path/to/script.sh
.
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
.
是否可以使用 date +"%M"
以分钟为单位获取系统时间,然后设置一个 cron 作业在 date +"%M"
运行代码>加上 5 分钟?
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?
我知道我可以通过以下过程获得 date +"%M"
+ 5:
I know I can get date +"%M"
+ 5 via the following process:
$ MIN=`date +"%M"`
$ export MIN
$ expr $MIN + 5
是否可以使用它来设置 cron 作业或脚本以在当前时间以分钟为单位"加上X 分钟"运行?
Is it possible to use this to set a cron job or script to run at "current time in minutes" plus "X minutes"?
我可以想象这在用户创建新文档并在创建文档 X 分钟后提示保存或命名文档的应用程序中很有用.
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.
推荐答案
您应该改用 at
命令.
使用at
,您可以使用时间或什至midnight
、teatime
、 等关键字来指定应该运行命令的时间>明天
等等.
With at
, you can specify the time when a command should be run using time or even keywords like midnight
, teatime
, tomorrow
etc..
您可以像这样指定 5 分钟后的时间:
You can specify the time after 5 min like this:
at now + 5 min
然后输入您要安排的命令.或者,您可以在作业文件中输入计划作业,并使用 -f
选项将其作为 at
命令的参数提供.
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.
作业文件示例:
$ cat myjobs.txt
/path/to/a/shell-script.sh
/path/to/any/command/or/script.sh
以下命令将在 5 分钟后执行这些作业:
The following command will execute those jobs after 5 mins:
$ at -f myjobs.txt now + 5 min
查看此链接了解更多信息信息.
Check this link for more information.
这篇关于是否可以将 cron 作业或 bash 脚本设置为从现在开始运行 X 分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!