如何每1小时1分钟设置crontab [英] How to set crontab every 1 hour 1 minute
问题描述
我想每1小时1分钟安排一次命令.例如,如果第一个命令在下午01:01执行,则下一个命令将在01:02 PM执行;否则,下一个命令将在01:02 PM执行.命令执行之间的时间为1小时1分钟.
I want to schedule a command every 1 hour and 1 minute. For example, if the first command executes at 01:01 pm, the next command will execute at 01:02PM; the time between the command executions is 1 hour and 1 minute.
我尝试使用
*/1 */1 * * *
但是它每分钟运行一次.谁能帮我吗?
but it runs every minute. Can anyone help me?
推荐答案
crontab中无法安排每61分钟运行一次作业(顺便说一句,这很奇怪),但是您可以执行它是间接的.
There's no way in crontab to schedule a job to run every 61 minutes (which, BTW, is an odd thing to want to do), but you can do it indirectly.
您可以安排作业每分钟运行一次:
You can schedule a job to run every minute:
* * * * * wrapper_script
其中 wrapper_script
仅在当前分钟是61的倍数时才调用所需的命令,如下所示:
where wrapper_script
invokes the desired command only if the current minute is a multiple of 61, something like this:
#!/bin/bash
second=$(date +%s)
minute=$((second / 60))
remainder=$((minute % 61))
if [[ $remainder == 0 ]] ; then
your_command
fi
这会将 $ minute
设置为自Unix纪元1970-01-01 00:00:00 UTC以来的分钟数.您可以在比较中使用 0
以外的值来调整命令运行的时间.
This sets $minute
to the number of minutes since the Unix epoch, 1970-01-01 00:00:00 UTC. You can adjust when the command runs by using a value other than 0
in the comparison.
这是假设您希望它每61分钟运行一次(这是您所要求的).但是,如果您想每天重复一次,那么它会在00:00、01:01,...,23:23上运行,然后在第二天的00:00再次运行,您可以直接在crontab中执行此操作:
That's assuming you want it to run every 61 minutes (which is what you asked). But if you want to repeat in a daily cycle, so it runs at 00:00, 01:01, ..., 23:23, and then again at 00:00 the next day, you can do it directly in crontab:
0 0 * * * your_command
0 0 * * * your_command
1 1 * * * your_command
2 2 * * * your_command
# ...
21 21 * * * your_command
22 22 * * * your_command
23 23 * * * your_command
这篇关于如何每1小时1分钟设置crontab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!