如何使用cron每天只运行一次脚本而不指定执行时间

ECI*_*CII 7 cron

我想每天运行一次备份脚本,但我不想指定某个时间,每天只运行一次。我读到 @daily cron 属性每天在 00:00 运行,但我不知道接收服务器此时是否会启动。

是否可以配置cron为每天只运行一次此脚本,而不管时间如何?或者运行脚本直到它成功,然后第二天再试一次?

@reboot 将是一个选项,但是发送端可能每天重新启动几次,在这种情况下会导致不必要的备份。

use*_*687 14

您可能想要使用 anacron。

从它的手册页

NAME
       anacron - runs commands periodically

SYNOPSIS
       anacron [-s] [-f] [-n] [-d] [-q] [-t anacrontab] [-S spooldir] [job] ...
       anacron [-S spooldir] -u [-t anacrontab] [job] ...
       anacron [-V|-h]
       anacron -T [-t anacrontab]

DESCRIPTION
       Anacron  can  be  used  to  execute  commands periodically, with a frequency specified in days.  Unlike
       cron(8), it does not assume that the machine is  running  continuously.   Hence,  it  can  be  used  on
       machines  that  aren't running 24 hours a day, to control daily, weekly, and monthly jobs that are usu?
       ally controlled by cron.
Run Code Online (Sandbox Code Playgroud)

Ubuntu 社区 HowTo:https : //help.ubuntu.com/community/CronHowto

使用 anacron:

  1. 简而言之可执行脚本文件中/etc/cron.hourly/etc/cron.daily/etc/cron.weekly/etc/cron.monthly

  2. 触摸脚本文件:

    sudo touch /etc/cron.daily/scriptfile
    
    Run Code Online (Sandbox Code Playgroud)

    cron.daily如果是日常工作)。

测试 anacron:

sudo anacron -f -d
Run Code Online (Sandbox Code Playgroud)
   -f     Force execution of the jobs, ignoring the timestamps.
   -d     Don’t fork to the background.  In this mode, Anacron will output
          informational  messages to standard error, as well as to syslog.
          The output of jobs is mailed as usual.
Run Code Online (Sandbox Code Playgroud)

日志中的 Anacron 消息:

grep anacron /var/log/syslog
Run Code Online (Sandbox Code Playgroud)

Anacron 将时间戳保存在/var/spool/anacron/. 如果您删除cron.dailycron.weeklycron.monthly从那里文件,anacron的将在下次启动时执行每天,每周或每月的脚本。