时间:2010-07-26 00:28:13

标签: cron bash

6 个答案:

答案 0 :(得分:8)

答案 1 :(得分:2)

答案 2 :(得分:1)

有些cron@reboot时间说明符(这涵盖了所有时间和日期字段)。如果你的,你可以使用它。如果这是一个“系统”服务(而不是为自己运行的东西),这里的其他解决方案可能更好。

答案 3 :(得分:1)

初始化脚本在启动时很好,但是不检测进程是否失败并且必须重新启动。 supervisord可以很好地检测失败的进程并重新启动它们。我建议使用像@Tim描述的5秒循环的脚本,但在它周围包裹supervisord以确保它继续运行。

答案 4 :(得分:0)

my answer to a similar question中详细说明的那样,您可以将SystemD计时器单位与所需的任何时间表一起使用-精确到理论上的1纳秒时间表没有sleep麻烦 < / p>

快速概述:

  1. 设置SystemD服务以运行所需的内容-这可以很简单:

/home/myusuf3/.config/systemd/user/makemehappy.service

[Unit]
Description=Make me happy
[Service]
ExecStart=/home/myusuf3/.local/bin/makemehappy.sh
  1. 根据man systemd.timer中的说明,使用所需的时间表设置SystemD计时器:

/home/myusuf3/.config/systemd/user/makemehappy.timer

[Unit]
Description=Schedule to make me happy
[Timer]
OnBootSec=5
OnUnitActiveSec=5
AccuracySec=1
  1. 启用并启动计时器:

systemctl --user daemon-reload
systemctl --user enable makemehappy.timer
systemctl --user start makemehappy.timer

(启用它后,它将在每次启动计算机时自动启动,但您可能仍想立即启动它)。

答案 5 :(得分:0)

要回答标题中的问题,这是如何每5秒运行一次cronjob:

* * * * * /path/to/script.sh
* * * * * sleep 5 && /path/to/script.sh
* * * * * sleep 10 && /path/to/script.sh
* * * * * sleep 15 && /path/to/script.sh
* * * * * sleep 20 && /path/to/script.sh
* * * * * sleep 25 && /path/to/script.sh
* * * * * sleep 30 && /path/to/script.sh
* * * * * sleep 35 && /path/to/script.sh
* * * * * sleep 40 && /path/to/script.sh
* * * * * sleep 45 && /path/to/script.sh
* * * * * sleep 50 && /path/to/script.sh
* * * * * sleep 55 && /path/to/script.sh

它并不漂亮,但是此解决方案没有额外的工具或依赖项。因此,如果您有正在执行的cron作业,则应该可以立即使用。