是否可以将cron作业或bash脚本设置为从现在开始运行X分钟? [英] Is it possible to set a cron job or bash script to run X minutes from now?

查看:56
本文介绍了是否可以将cron作业或bash脚本设置为从现在开始运行X分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以通过添加以下代码行来设置来使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分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆