foo.service
還處於執行狀態,下一時間步到了也不會重複執行 foo.service
,可以避免重複執行造成 process 壘加,導致系統異常
foo.service
,當 09 點 10 分到了,結果 09 點 05 分的 foo.service
還在執行中,foo.timer
判斷 foo.service
還是處於activating
狀態,就不會再啟動 foo.service
foo.service
承接 Linux 的功能,比如限制使用的 CPU 和記憶體等
# 用戶自定義檔案,也是接下來會用到路徑
/etc/systemd/system/*
# 通常為系統安裝套件預設放置位置 (ex: deb, rpm...)
/usr/lib/systemd/system/*
# 立即執行 or 停止指定的 service
systemctl {start,stop} foo.service
# 啟用 or 取消開機時會自動執行指定的 service
systemctl {enable,disable} foo.service
# 重啟指定的 service
systemctl restart foo.service
# 當有編輯 systemd,記得 reload
systemctl daemon-reload
/var/log/messages
systemctl
指令操作時,若沒有加結尾,預設你是指 .service
舉例如下,以下是相同指令
systemctl status foo
systemctl status foo.service
*.timer
,*.timer
會去執行和自己同樣名稱的 *.service
。如 foo.timer
時間到會去執行foo.service
/opt/foo.sh
foo.service
和 foo.timer
放底下指定路徑/etc/systemd/system/foo.service
[Service]
Type=oneshot
ExecStart=/opt/foo.sh
[Install]
WantedBy=multi-user.target
/etc/systemd/system/foo.timer
[Timer]
OnBootSec=5min
OnUnitActiveSec=5min
AccuracySec=1min
[Install]
WantedBy=timers.target
systemctl daemon-reload
/etc/systemd/system/foo.service
Type=oneshot #oneshot 是指執行一次
ExecStart=/opt/foo.sh #這邊指定你要執行的腳本路徑,權限要可執行
/etc/systemd/system/foo.timer
OnBootSec=5min #開機後 5 分鐘執行
OnUnitActiveSec=5min #每 5 分鐘執行一次
AccuracySec=1min #精確到分鐘
foo.service
,確認能成功執行
# start
systemctl start foo.service
# check status
systemctl status foo.service
foo.timer
,當設定的每 5 分鐘時間到了,foo.timer
會自動執行 foo.service
# start
systemctl start foo.timer
foo.timer
執行的狀態 (很重要的功能)systemctl list-timers
systemctl enable foo.timer