我认为有趣的是,如果您确实希望每 86410 秒重新启动一次处理,那么您的服务开始时间会在几天之后,每次都在稍后和更晚 - 所以如果您最初安排您的流程在 08 开始:00,大约一年后从 09:00 开始,大约 23.6 年后,它将昼夜不停地在早上 8 点重新开始。
Cron 绝对不是为这种事情设计的 :-)
但是,如果您在最近的 Linux 操作系统上运行,则可以使用 SystemD 计时器单元来执行此操作。您可能熟悉 SystemD 服务单元 - 因为这是您为现代 Linux 编写服务的方式,但 SystemD 可以做的更多,其中之一就是安排需要有趣时间表的事情。 p>
假设您将处理作业作为 SystemD 服务运行,它可能看起来像这样:
/etc/systemd/system/data-processing.service
[Unit]
Description=Process some data
[Service]
Type=simple # its the default, but I thought I'd be explicit
ExecStart=/usr/bin/my-data-processor
然后您可以非常简单地设置一个计时器单元以每 86410 秒启动一次此服务 - 在 /etc/systemd/system/data-processing.timer 中创建一个包含此内容的计时器单元文件:
[Unit]
Description=start processing every day and 10 seconds
[Timer]
OnBootSec=0 # Start immediately after bootup
# Start the next processing 86410 seconds after the last start
OnUnitActive=86410
AccuracySec=1 # change from the default of 60, otherwise
# the service might start 86460 after the last start
[Install]
WantedBy=timers.target
然后只需启用并启动计时器单元 - 而不是服务。如果服务已启用,您可能还想禁用它 - 计时器将根据需要负责运行它。
systemctl daemon-reload
systemctl enable data-processing.timer
systemctl start data-processing.timer
再看一遍,您提到要在上一次运行完成后开始下一次运行服务。如果完成处理的时间不正好是 86400 秒,会发生什么?如果我们将要求更改为“在完成运行后重新启动数据处理服务,但先让其冷却 10 秒”,那么您根本不需要计时器 - 您只需要让 SystemD 重新启动服务后10 秒冷却时间,无论何时完成。
我们可以改变上面的服务单元来做到这一点:
[Unit]
Description=Process some data
[Service]
Type=simple
ExecStart=/usr/bin/my-data-processor
Restart=always
RestartSec=10