每3天后逐字执行bash脚本

我想每隔3天执行一次shell脚本。 使用带有01 00 */3 * * crontab实际上不会满足条件,因为它将在31日运行,然后在1个月的第1天运行。 */3语法与1,4,7 … 25,28,31相同。

应该有办法让脚本本身检查条件并在3天没有通过时退出。 所以crontab每天都会执行脚本,但脚本本身会检查是否已经过了3天。

我甚至找到了一些代码,但它给了我语法错误,任何帮助将不胜感激。

 if (! (date("z") % 3)) { exit; } main.sh: line 1: syntax error near unexpected token `"z"' main.sh: line 1: `if (! (date("z") % 3)) {' 

要在上次执行尚未至少特定时间之前立即中止并退出脚本,您可以使用此方法,该方法需要存储上次执行日期和时间的外部文件。

将这些行添加到Bash脚本的顶部:

 #!/bin/bash # File that stores the last execution date in plain text: datefile=/path/to/your/datefile # Minimum delay between two script executions, in seconds. seconds=$((60*60*24*3)) # Test if datefile exists and compare the difference between the stored date # and now with the given minimum delay in seconds. # Exit with error code 1 if the minimum delay is not exceeded yet. if test -f "$datefile" ; then if test "$(($(date "+%s")-$(date -f "$datefile" "+%s")))" -lt "$seconds" ; then echo "This script may not yet be started again." exit 1 fi fi # Store the current date and time in datefile date -R > "$datefile" # Insert your normal script here: 

不要忘记将有意义的值设置为datefile=并根据需要调整seconds=的值( $((60*60*24*3))评估为3天)。


如果您不想要单独的文件,还可以将上次执行时间存储在脚本的修改时间戳中。 这意味着,对脚本文件进行任何更改都将重置3计数器,并将其视为脚本成功运行。

要实现它,请将下面的代码段添加到脚本文件的顶部:

 #!/bin/bash # Minimum delay between two script executions, in seconds. seconds=$((60*60*24*3)) # Compare the difference between this script's modification time stamp # and the current date with the given minimum delay in seconds. # Exit with error code 1 if the minimum delay is not exceeded yet. if test "$(($(date "+%s")-$(date -r "$0" "+%s")))" -lt "$seconds" ; then echo "This script may not yet be started again." exit 1 fi # Store the current date as modification time stamp of this script file touch -m -- "$0" # Insert your normal script here: 

同样,不要忘记根据您的需要调整seconds=的值( $((60*60*24*3))评估为3天)。

Cron确实是错误的工具。 实际上有一个常用的和不受欢迎的工具可以使用。 at 主要是为交互式使用而设计的,我相信有人会找到更好的方法来做到这一点。

在我的情况下,我将在testjobs.txt中列出我正在运行的脚本,并包含一行读取。

作为一个例子,我将它作为testjobs.txt

 echo "cat" >> foo.txt date >> foo.txt at now + 3 days < testjobs.txt 

我有两个无辜的命令,可能是你的shellcripts。 我运行echo以确保我有一个确定的输出,并确认命令运行的日期根据需要运行。 在运行此命令时,它将通过向at添加新作业3天来完成。 (我测试了一分钟 - 哪个有效)

我很确定我会因为我滥用的方式而被召唤出来,但它是一个方便的工具,用于安排在上一个命令之后或x天后运行的命令。

首先,上面的代码片段是无效的Bash语法,看起来像Perl。 其次, datez参数使其输出数字时区。 +%j是日期编号。 你需要:

 if [[ ! $(( $(date +%j) % 3 )) ]] ;then exit fi 

但你还是会在年底看到陌生感:

 $ for i in 364 365 1 ; do echo "Day $i, $(( $i % 3 ))"; done Day 364, 1 Day 365, 2 Day 1, 1 

您可以更好地将计数保存在文件中,并测试/更新它。

如果您可以让脚本永久运行,您可以执行以下操作:

 while true; do [inert code here] sleep 259200 done 

此循环始终为true,因此它将始终执行代码,然后等待三天再重新开始循环。

您可以使用anacron而不是cron,它的设计完全符合您的需要。 从联机帮助页:

Anacron可用于定期执行命令,频率以天为单位指定。 与cron(8)不同,它不假设机器连续运行。 因此,它可以在一天24小时不运行的机器上使用,以控制通常由cron控制的每日,每周和每月工作。

执行时,Anacron从配置文件中读取作业列表,通常是/ etc / anacrontab(请参阅anacrontab(5))。 此文件包含Anacron控制的作业列表。 每个作业条目指定以天为单位的句点,以分钟为单位的延迟,唯一作业标识符和shell命令。

对于每个作业,Anacron会检查此作业是否在最近n天内执行,其中n是为该作业指定的时间段。 如果没有,Anacron在等待指定为delay参数的分钟数后运行作业的shell命令。

退出命令后,Anacron将日期记录在该作业的特殊时间戳文件中,以便它可以知道何时再次执行该作业。 只有日期用于时间计算。 不使用小时。