如何将 cron 作业配置为每 2 天晚上 11 点运行一次

fre*_*set 19 linux centos cron

我有一个 centos 服务器,我想每两天晚上 11 点在它上面运行一个作业,我该怎么做?

eww*_*ite 35

您可以使用以下 cron 安排。字段表示(从左到右):
分钟、小时、月中的某天、月、周中的某天。月份字段中的“*/2”表示“每两天”。

0 23 */2 * * insert_your_script_here.sh

  • 我的 RHEL 的 'man -s 5 crontab' 说 '*' 是 'first-last',这会将 '*/2' 扩展为 '1,3,5,7...29,31'。我认为这意味着脚本将在 31 天的第 31 个月和第一个月份运行 31 天,我认为这不是预期的行为。 (8认同)

Mic*_*haw 11

一般情况下,需要使用 crontab 来定义任务和运行计划。

例如

crontab -e -u root
Run Code Online (Sandbox Code Playgroud)

这将使您进入 VI 编辑 root 的 crontab 条目。然后正如 ewwhite 所说,输入:

0 23 */2 * * insert_your_script_here.sh
Run Code Online (Sandbox Code Playgroud)

然后 [^ESC] ZZ 保存更改。

这是一个很好的第一次尝试,但这并不是每隔一天,因为它会在当月的 30 日运行,然后在当月的 2 日运行。如果您确实需要每隔 2 天运行一次,请每晚运行该脚本。

0 23 * * * insert_your_script_here.sh
Run Code Online (Sandbox Code Playgroud)

并在脚本开始时使用

#!/bin/sh
if [ -f /tmp/altday.txt ]; then
  rm /tmp/altday.txt
  exit
fi
touch /tmp/altday.txt
Run Code Online (Sandbox Code Playgroud)

这使用文本文件强制脚本退出每个替代调用。