cron 每两周执行_如何每两周/月/天执行一次cron作业

cron 每两周执行

We may want to run some jobs for every two weeks/months/days… under some situation such as backing up for every other week. In addition, we may add more complex rules for running jobs, e.g. run a command when the load of the server is higher than a certain level. With the help of the shell programming language we can easily achieve this goal.

在某些情况下,例如每隔一周备份一次,我们可能希望每两周/月/天运行一些工作。 此外,我们可能会为运行作业添加更复杂的规则,例如,当服务器的负载高于特定水平时运行命令。 借助Shell 编程语言,我们可以轻松实现此目标。

crontab的基础 (Basics of the crontab)

Crontab file controls how and what to run by cron. The crontab line has a format as follows:

Crontab文件控制cron的运行方式和运行方式。 crontab行的格式如下:

.---------------- minute (0-59)
|  .------------- hour (0-23)
|  |  .---------- day of month (1-31)
|  |  |  .------- month (1-12) OR jan,feb,mar,apr ...
|  |  |  |  .---- day of week (0-6) (Sunday=0 or 7) OR sun,mon,tue ...
|  |  |  |  |
*  *  *  *  *  command to be executed

To add a cron job for your current user, run

要为当前用户添加cron作业,请运行

$ crontab -e

And edit the crontab file by adding/deleting lines. Check the crontab man page for more details.

然后通过添加/删除行来编辑crontab文件。 有关更多详细信息,请参见crontab手册页

The cron job is run as a piece of shell code. /bin/sh is the shell by default. You may change it to some other shells like bash by adding a like like SHELL=/bin/bash at the beginning of the crontab file.

cron作业作为外壳程序代码运行。 /bin/sh是默认的shell。 您可以通过在crontab文件的开头添加诸如SHELL=/bin/bash类的名称,将其更改为诸如bash之类的其他外壳程序。

方法1:使用crontab扩展 (Method 1: use crontab extensions)

crontab provides some extensions to support this: ranges can include "steps" like /2. 1-9/2 is the same as 1,3,5,7,9. This can be the easiest method if the job is intended to be run every two N in a range such as N is "day" and the range is "days in a month". Check the crontab man page for more details about ranges and steps of crontab.

crontab提供了一些扩展来支持此功能:范围可以包含/2类的“步骤”。 1-9/21,3,5,7,9相同。 如果打算在范围N(例如,N为“天”,范围为“一个月中的天”)中每两个N运行一次作业,则这是最简单的方法。 检查crontab手册页以获取有关crontab的范围和步骤的更多详细信息。

An example.

一个例子。

To run the command every two months at 1:00 on the first day of the month, the crontab is

要在每月第一天的1:00每两个月运行一次命令,则crontab为

0 1 1 */2 * command to be executed

But please be aware that the every two N here is not exactly every two N. For days, it is every two days in a month. For months, it is every two months in a year. If you need to make sure your cron job is run exactly every two N, check the other methods.

但是请注意,这里每两个N并不完全是每两个N。对于几天,它是一个月中的每两天。 几个月以来,一年两次。 如果需要确保您的cron作业每两个N准确运行一次,请检查其他方法。

方法2:使用测试语句来控制是否真正运行命令 (Method 2: use a test statement to control whether to really run the command)

The command executed for a cron job is a piece of shell code. So you can make the command run every day/week/month while use a test statement to control whether to really run the command.

为cron作业执行的命令是一段Shell代码。 因此,您可以使命令每天/每周/每月运行,同时使用测试语句来控制是否真正运行该命令。

Let’s take an example to illustrate the idea. Now, we want to run a job every 2 days. As there are months having 31 days, some jobs are not run exactly every 2 days if we use method 1 above. We can have a cron job as follows to make sure it runs exactly every 2 days (86400 seconds) at 1:00.

让我们以一个例子来说明这个想法。 现在,我们希望每2天执行一次工作。 由于有31个月的月份,因此,如果使用上述方法1,则某些作业可能不会每2天准确运行一次。 我们可以按照以下步骤进行cron作业,以确保它每2天(86400秒)在1:00准确运行一次。

0 1 * * * < $((($(date +%s) / 86400) % 2)) == 0 > && /path/to/your/command

Here, the shell script < $((($(date +%s) / 86400) % 2)) == 0 > && /path/to/your/command is run every day. But your command is run only when $((($(date +%s) / 86400) % 2)) == 0. That is when the number of days since 1970-01-01 00:00:00 UTC is divisible by 2.

在这里,shell脚本< $((($(date +%s) / 86400) % 2)) == 0 > && /path/to/your/command每天运行< $((($(date +%s) / 86400) % 2)) == 0 > && /path/to/your/command 。 但是仅在$((($(date +%s) / 86400) % 2)) == 0 。 那就是自1970-01-01 00:00:00 UTC以来的天数可以被2整除。

With the help of date, we can make make test statements. Let’s take another example, run a job exactly every 5 months starting from March of 2016. The cron tab entry can be

date的帮助下,我们可以进行测试。 让我们再举一个例子,从2016年3月开始,每5个月执行一次作业。cron选项卡条目可以是

0 1 1 * * < $(((($(date +%Y) - 2016) * 12 + $(date +%m) - 3) % 5)) == 0 > && /path/to/your/command

Only when the number of months since March 2016 is divisible by 5, the command is run.

仅将自2016年3月以来的月份数除以5时,才运行该命令。

方法3:使用状态保存在磁盘上的Shell脚本 (Method 3: use a shell script with its state saved on disk)

The idea is to invoke a shell script every day/week/month and the shell judges whether to run the job. The key idea here is that the shell uses a file to mark its state.

这个想法是每天/每周/每月调用一个Shell脚本,然后Shell判断是否运行该作业。 这里的关键思想是外壳程序使用文件来标记其状态。

The shell code:

外壳代码:

#!/bin/bash

# a file marking the state on disk
mark_file=$HOME/.job-run-marker-1

# check whether the job run last time it is invoked
if [ -e $mark_file ] ; then
    rm -f $mark_file
else
    touch $mark_file
    exit 0
fi

# job command is here

The script will not find $mark_file on the first run, so it will create it and exit. On the second run the script will remove $mark_file and then proceed to execute the job command. For the third run, it is the same as the first run. So if this script is run weekly by cron, the job command will run every two weeks.

该脚本在第一次运行时找不到$ mark_file,因此它将创建它并退出。 在第二次运行时,脚本将删除$ mark_file,然后继续执行job命令。 对于第三次运行,它与第一次运行相同。 因此,如果此脚本由cron每周运行一次,则job命令将每两周运行一次。

You can extend the shell with more complex logic like storing the last run date/time in the $mark_file or check more system information like the CPU/mem/disk load.

您可以使用更复杂的逻辑扩展外壳程序,例如将上次运行日期/时间存储在$ mark_file中,或者检查更多系统信息,例如CPU /内存/磁盘负载。

An example is as follows.

一个例子如下。

We want to back up Xen DomU VMs for every two weeks. We create a script /home/share/bin/xen-bak. The beginning part of this script is like what we list above.

我们希望每两周备份一次Xen DomU VM。 我们创建一个脚本/ home / share / bin / xen-bak。 该脚本的开始部分类似于我们上面列出的内容。

The line for the job is as follows.

作业行如下。

0 2 * * 2 /home/share/bin/xen-bak

The backup command will run at 2:00 for every other Tuesday.

该备份命令将在其他每个星期二的2:00运行。

翻译自: https://www.systutorials.com/how-to-run-a-cron-job-every-two-weeks-months-days/

cron 每两周执行