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 ] ; thenrm -f $mark_file
elsetouch $mark_fileexit 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 每两周执行

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

  1. 连续两天高烧_连续工作两天,可以看电视11秒

    连续两天高烧 by James Barnard 詹姆斯·巴纳德(James Barnard) 连续工作两天,可以看电视11秒 (Two days of work for 11 seconds of T ...

  2. 一个显示器分屏显示两个画面_测了两个爆款游戏显示器,结果我发现他们都有坑。...

    赶着双十二的尾巴, 2000 元价位最划算的 2K 27 寸游戏显示器大结局,来啦!上一次我们介绍了小米显示器 27 寸 165 Hz 版,错过了的小伙伴可以先点这里回顾一下.几句话总结一下:虽然它的 ...

  3. 一个显示器分屏显示两个画面_测了两个爆款游戏显示器,结果我发现他们都有坑...

    2000 元价位最划算的 2K 27 寸游戏显示器大结局,来啦! 上一次我们介绍了小米显示器 27 寸 165 Hz 版,错过了的小伙伴可以先点这里回顾一下.便宜到离谱的小米显示器,真的就香吗? 几句 ...

  4. 计算机两个硬盘那个快,固态硬盘可以装两个吗_电脑装两个固态会快吗

    很多人问我,电脑可不可以装两个固态硬盘?装两个固态硬盘电脑会更快吗?电脑是可以装两个固态硬盘的,而且装了之后电脑的运行速度会比之前快.下面小编就给大家分享一些电脑装两个固态硬盘的小知识. 具体如下: ...

  5. jQuery 一次定时器_为什么JVM每隔一小时执行一次Full GC?

    随着接口自动化监控的完善,线上大多数的接口都被自动化监控系统接入,因此开发.测试人员可以及时的了解API的健康情况,监控内容包括检查API的状态码是否正常.返回的内容断言是否成功等,异常的接口会对外发 ...

  6. mysql查询财两个人信息_春 东财《MySQL数据库系统及应用》在线作业二(随机)

    <春 东财<MySQL数据库系统及应用>在线作业二(随机)>由会员分享,可在线阅读,更多相关<春 东财<MySQL数据库系统及应用>在线作业二(随机)(112 ...

  7. cron表达式 每隔55分钟_如何用crontab每5分钟执行一次

    展开全部 一.创建存放shell脚本的文件夹 [root@data]# mkdir shell 二.编写shell脚本 使用vim.nano.vi等任意文本编辑工具,都可以编写shell脚本.使626 ...

  8. kafka 两段式提交_如何理解两阶段提交?

    在分布式系统中,为了让每个节点都能够感知到其他节点的事务执行状况,需要引入一个中心节点来统一处理所有节点的执行逻辑,这个中心节点叫做协调者(coordinator),被中心节点调度的其他业务节点叫做参 ...

  9. 一台电脑怎么接两个显示器_电脑数码类目显示器 篇二:11.11抄作业,个人消费级显示器怎么选--20款好价显示器推荐_显示器...

    2020-11-09 21:56:2572点赞390收藏91评论 想攒一台电竞主机.家用主机.酷炫主机无从下手?想省钱又怕性能不达标?值得买帮你打造定制化DIY装机工具,自助全网比价装机,提供最适合的 ...

最新文章

  1. 数据结构c语言函数大全,数据结构习题库(c语言版).doc
  2. verycd重整——linux教程
  3. react实战项目_前端学习路线图--从网页设计到项目开发
  4. android sharesdk分享功能,Android ShareSDK快速实现分享功能
  5. Life Cycle Stages of ASP.NET Web Page.
  6. Cannot add foreign key constraint 错误
  7. 百度云服务器最新活动,百度云服务器1折_云服务器超值优惠_特价云服务器促销活动-天互数据...
  8. ImportError: No module named matplotlib.pyplot
  9. html两个自然段怎么写,春天作文二个自然段
  10. magisk mask面具自动打卡
  11. 分布式文件存储SeaweedFS试用对比总结
  12. Xposed环境安装
  13. 使用000webhost.com免费主机搭建一个WordPress站点
  14. 基于学术研究下载NOAA气象雷达资料的详细步骤
  15. 关闭PyCharm的Run with Python Console模式
  16. Java-(二)微信小程序授权获取用户信息和手机号码
  17. 点歌服务器定时关闭系统,iOS 基础教程:设置系统自带的睡眠计时器,定时关闭音乐播放...
  18. stormmedia文件夹,360downloads文件夹,FavoriteVideo文件夹是干什么的?
  19. 《赵成的运维体系管理课》学习笔记(3)——云计算时代的运维实践
  20. 眼动和脑波相关性分析和回归分析

热门文章

  1. android 即时战略游戏,即时战略手游排行榜前十名 即时战略游戏推荐
  2. 神通数据库 mysql_sqlserver、mysql、oracle、神通数据库(oscar)、达梦(dm)各自的默认端口号...
  3. 【老生谈算法】matlab实现控制系统稳定性——控制系统
  4. 手把手的教你用MapABC的地图API制作自己的免费地图
  5. arduino教程-07.舵机
  6. Visio 保存高质量的PDF
  7. 【java】三角形(Object 类与异常处理)
  8. ElasticSearcho从入门到放弃:(三)Beats
  9. 【计算机网络】【网络层:控制平面-5】
  10. 视音频编解码基本术语及解释MediaInfo