測試環境為 CentOS 8 x86_64 (虛擬機)
參考文章 – https://wiki.archlinux.org/title/Systemd/Timers
Systemd unit configuration 檔案以 .timer 為結尾的就是用於控制 .service 文件或事件,建議透過這種方式來取代傳統的 (crontab) cron table,下面來看一下要如何編寫 .service 與 .timer 檔案.
參考範例 – https://unix.stackexchange.com/questions/198444/run-script-every-30-min-with-systemd
以下程式需要每 10 秒執行一次.
[root@localhost ~]# vi /tmp/1.sh #!/bin/bash echo `date` >> /tmp/2
首先編輯 Systemd unit configuration 檔案 (一般使用者寫的 service 檔案放在 /etc/systemd/system/ ,系統的放在 /usr/lib/systemd/system)
[root@localhost ~]# vi /etc/systemd/system/test.service [Unit] Description=test job [Service] Type=oneshot ExecStart=/bin/bash /tmp/1.sh
說明:
[Unit]
- Description 敘述該 Systemd Unit Files 目的.
[Service]
- Type – oneshot : This type is similar to simple, but the process exits before starting consequent units.
- ExecStart – 指定執行程式.
Note:
在 [Service] 區間可以透過 RuntimeMaxSec=8h 來指定程式需執行多久的時間後停止,時間單位請參考 https://www.freedesktop.org/software/systemd/man/latest/systemd.time.html#Parsing%20Time%20Spans 說明.
剛程式不會定期執行,還需編輯 .timer 檔案.
[root@localhost ~]# vi /etc/systemd/system/test.timer [Unit] Description=test [Timer] OnUnitActiveSec=10s OnBootSec=10s [Install] WantedBy=timers.target
說明:
- OnUnitActiveSec : Defines a timer relative to when the unit the timer unit is activating was last activated.
- OnBootSec : Defines a timer relative to when the machine was booted up.
- WantedBy : A list of units that weakly depend on the unit, 這邊就是須等待 timers.target 服務啟動後才會啟動.
[root@localhost ~]# systemctl list-dependencies --before test.timer test.timer ● ├─test.service ● ├─shutdown.target ● └─timers.target
啟動 test.timer 服務.
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start test.timer
[root@localhost ~]# systemctl enable test.timer
檢視服務狀態.
[root@localhost ~]# systemctl status test ● test.service - test job Loaded: loaded (/etc/systemd/system/test.service; static; vendor preset: disabled) Active: activating (start) since Fri 2022-08-19 01:37:26 PDT; 2ms ago Main PID: 28234 (bash) Tasks: 0 (limit: 49280) Memory: 0B CGroup: /system.slice/test.service ├─28234 /bin/bash /tmp/1.sh └─28235 date Aug 19 01:37:26 localhost.localdomain systemd[1]: Starting test job...
[root@localhost ~]# systemctl status test.timer ● test.timer - test Loaded: loaded (/etc/systemd/system/test.timer; disabled; vendor preset: disabled) Active: active (running) since Thu 2022-08-18 23:36:35 PDT; 2h 1min ago Trigger: n/a Aug 18 23:36:35 localhost.localdomain systemd[1]: Started test.
檢視程式是否有正確執行.
[root@localhost ~]# systemctl list-timers --all NEXT LEFT LAST PASSED UNIT ACTIVATES Fri 2022-08-19 02:22:52 PDT 42min left Fri 2022-08-19 00:42:36 PDT 57min ago dnf-makecache.timer dnf-makecache.service Sat 2022-08-20 00:00:00 PDT 22h left Fri 2022-08-19 00:00:36 PDT 1h 39min ago unbound-anchor.timer unbound-anchor.service Sat 2022-08-20 01:33:36 PDT 23h left Fri 2022-08-19 01:33:36 PDT 6min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service n/a n/a Fri 2022-08-19 01:39:53 PDT 4ms ago test.timer test.service 4 timers listed.
資料的確有寫入到 /tmp/2 檔案.
[root@localhost ~]# cat /tmp/2 Thu Aug 18 23:36:35 PDT 2022 Thu Aug 18 23:36:56 PDT 2022 Thu Aug 18 23:37:56 PDT 2022 Thu Aug 18 23:38:56 PDT 2022 Thu Aug 18 23:39:36 PDT 2022 Thu Aug 18 23:39:56 PDT 2022 Thu Aug 18 23:40:56 PDT 2022
沒有解決問題,試試搜尋本站其他內容