如何使用 cron 守护进程调用脚本
我需要每两天调用一次 shell 脚本,我读到有关 cron 守护进程的信息,它可以帮助我定期调用脚本,所以你能举个例子,如何让我的脚本能够被 cron 守护进程调用。
I need to invoke my shell script every two days, I read about cron daemon that it can help me invoking scripts periodically, so can you give an example how can I make my script able to be invoked by cron daemon.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用 crontab -e 来调出 cron 编辑器
crontab 的格式为: MIN HOUR DAY MONTH DAYOFWEEK COMMAND
因此,要使脚本每 2 天运行一次,您需要:
完成后,输入
:x
保存并退出。然后,您可以运行 crontab -l (这是一个 ell)来确保它生效。*注意:如果你的 cron 守护进程每两天在偶数天(2,4,6..)或奇数天(1,3,5..)运行一次,它实际上有点模糊,并且它可能会根据如何切换这些当月有很多天。如果你想明确这一点,你可以这样做:
在奇数天运行
在偶数天运行
Invoke
crontab -e
to bring up the cron editorThe format for crontab is: MIN HOUR DAY MONTH DAYOFWEEK COMMAND
Therefore to make a script run every 2 days, you'll want:
Once you're done, type
:x
to save and quit. You can then runcrontab -l
(that's an ell) to make sure it took hold.*Note: It's actually a bit ambiguous if your cron daemon will run that every two days on even days (2,4,6..) or odd days (1,3,5..) and it may switch these depending on how many days are in the current month. If you want to unambigufy this, you can do this:
Run on Odd Days
Run on Even Days
上面的内容将在两天的上午 00:00:00 运行一次,
您可以执行
man 5 crontab
来获取一些有用的信息the above will run once in two days at 00:00:00am
you can do
man 5 crontab
to get some helpful information使用经典的 cron 无法每 48 小时精确执行一次,即使有多个选项卡条目也是如此。
每2天一次,平均每天0.5次。然而,其他人发布的“1-31/2”平均每天运行 0.509851 次,换句话说:在 400 年的时间范围内大约每 47 小时 4 分钟运行一次。 (因为它将在奇数月的 31 日和下个月的 1 日运行。)
编辑:“*/2”将运行 179x/年,即 0.49041,因此这也不准确。
公历万岁。
There is no way to execute exactly every 48 hours with the classical cron, not even with more than one tab entry.
Every 2 days is an average of 0.5 times per day. However, "1-31/2" as posted by others, runs 0.509851 times per day on average, in other words: approx every 47h 4min over a timeframe of 400 years. (Because it will run on both the 31st of an odd month, and the 1st of the following month.)
Edit: "*/2" would run 179x/year, which is 0.49041, so that is not exact either.
Hooray for the Gregorian calendar.