- 將二個單元檔(unit)置於 /lib/systemd/system
- 其中之一是 .service,是描述執行目的
- 另一是 .timer,是用以觸發該 service
- 此二者的名稱必須相同
- 描述時(被 systemd 解析時),必須使用絕對路徑
- 創建時,存取權限須留意
- 如下範例:
- 每隔 30 秒將當時時間寫入 time_records.log
- 而 service 是去執行 write_current_time.sh
- write_current_time.sh:
#! /usr/bin/env bash
echo "$(date)" >> /home/ken/tasks/time_records.log
- time_records.service:當啟用時,就會先被執行一次
[Unit]
Description=now time service
[Service]
ExecStart=/bin/bash /home/ken/tasks/write_current_time.sh
[Install]
WantedBy=multi-user.target
[Unit]
Description=run service at every 30s
[Timer]
OnUnitActiveSec=30s
Unit=time_records
[Install]
WantedBy=timers.target
- 備註:
複合指令的用法例:
ExecStart=/bin/sh -c ‘until ping -c1 google.com; do sleep 1; done;’
- 參考資料
- https://wiki.archlinux.org/title/Systemd/Timers
- https://man.archlinux.org/man/systemd.timer.5
- https://man.archlinux.org/man/systemd.service.5
- https://man.archlinux.org/man/systemd.1.zh_TW
- http://www.ruanyifeng.com/blog/2018/03/systemd-timer.html
- https://www.cnblogs.com/operationhome/p/10720067.html
- https://systemd.io/
- https://www.freedesktop.org/software/systemd/man/systemd.html
- https://www.freedesktop.org/software/systemd/man/systemd.directives.html
- https://www.freedesktop.org/software/systemd/man/systemd.unit.html
- https://www.freedesktop.org/software/systemd/man/systemd.service.html
- https://www.freedesktop.org/software/systemd/man/systemd.exec.html
- http://www.jinbuguo.com/systemd/systemd.service.html
- https://cloud.tencent.com/developer/article/1516125
- https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html
- https://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html
- https://www.digitalocean.com/community/tutorials/systemd-essentials-working-with-services-units-and-the-journal
- https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
- https://www.cloudnull.io/2019/04/running-services-in-network-name-spaces-with-systemd/