Cronjob,每20分钟一次。 重启后直接启动
像cronjob一样跑
*/15 * * * * sh /home/me/skript.sh >> /home/me/out.log 2>&1
将每15分钟执行一次skript.sh
。 在12:10启动计算机,我必须等待5分钟。
有没有办法让重新启动后立即启动cronjob然后每15分钟一次?
谢谢,马丁
系统救援
这对cron
是不可能的,但systemd
可以做到这一点。 您需要创建两个systemd单元,一个用于启动脚本,另一个用于计时器。
文件/etc/systemd/system/my-fifteen-minutes.timer
:
[Unit] Description=15 minute timer [Timer] # start this 0 minutes after boot: OnBootSec=0 min # ... and then every 15 minutes: OnActiveSec=15 min [Install] WantedBy=multi-user.target
文件/etc/systemd/system/my-fifteen-minutes.service
(注意不同的扩展名):
[Unit] Description=My script [Service] Type=oneshot ExecStart=/bin/sh -c "/home/me/skript.sh >> /home/me/out.log 2>&1" User=me
将这些文件放在/etc/systemd/system
目录中并启用计时器
# make systemd aware of them sudo systemctl daemon-reload # make sure the timer is engaged at startup sudo systemctl enable my-fifteen-minutes.timer # start the timer "now" (without rebooting): sudo systemctl start my-fifteen-minutes.timer # examine the status: systemctl status my-fifteen-minutes.timer my-fifteen-minutes.service
命令systemctl status my-fifteen-minutes.timer
将显示类似的内容
● my-fifteen-minutes.timer - 15 minute timer Loaded: loaded (/etc/systemd/system/my-fifteen-minutes.timer; enabled; vendor preset: enabled) Active: active (waiting) since Sun 2018-07-01 14:42:05 CEST; 1s ago Trigger: Sun 2018-07-01 14:57:05 CEST; 14min left Jul 01 14:42:05 stratum9 systemd[1]: Started 15 minute timer.
这意味着: 计时器在1s ago
被触发,并将在约14分钟内再次触发(在“Sun 2018-07-01 14:57:05 CEST”)。
请注意, 计时器和服务是两个不同的东西,您需要定义它们。 默认情况下, 计时器单元启动一个具有相同名称的服务单元 (扩展名.timer
与service
除外),即foo.timer
将控制foo.service
(但您可以覆盖它)。 计时器单元只定义服务单元 何时发生事件, 服务单元定义实际操作(在您的情况下:启动脚本/home/me/skript.sh
)。
进一步阅读:
- 计时器: https : //www.freedesktop.org/software/systemd/man/systemd.timer.html
- 服务: https : //www.freedesktop.org/software/systemd/man/systemd.service.html
- 单位(适用于计时器和服务): https : //www.freedesktop.org/software/systemd/man/systemd.unit.html