使用 systemd.timer 重複在指定時間執行作業

systemd 提供了 cron 的現代實現。要執行指令碼定期,需要服務和計時器檔案。服務和計時器檔案應放在/ etc / systemd / {system,user}中。服務檔案:

[Unit]
Description=my script or programm does the very best and this is the description

[Service]
# type is important!
Type=simple
# program|script to call. Always use absolute pathes 
# and redirect STDIN and STDERR as there is no terminal while being executed 
ExecStart=/absolute/path/to/someCommand >>/path/to/output 2>/path/to/STDERRoutput
#NO install section!!!! Is handled by the timer facitlities itself.
#[Install]
#WantedBy=multi-user.target

接下來的計時器檔案:

[Unit]
Description=my very first systemd timer
[Timer]
# Syntax for date/time specifications is  Y-m-d H:M:S 
# a * means "each", and a comma separated list of items can be given too
# *-*-* *,15,30,45:00  says every year, every month, every day, each hour,
# at minute 15,30,45 and zero seconds

OnCalendar=*-*-* *:01:00  
# this one runs each hour at one minute zero second e.g. 13:01:00