§2023-08-19
- Use systemd timers instead of cronjobsTimers provide finer-grained control of events than cronjobs By David Both (Correspondent) July 7, 2020
¶ 1. Show *.timer
$ systemctl status *.timer
● man-db.timer - Daily man-db regeneration
Loaded: loaded (/usr/lib/systemd/system/man-db.timer; disabled; preset: disabled)
Active: active (waiting) since Tue 2023-08-15 21:10:21 CST; 3 days ago
Trigger: Sat 2023-08-19 09:34:50 CST; 52min left
Triggers: ● man-db.service
Docs: man:mandb(8)
8月 15 21:10:21 orgpi5Arch systemd[1]: Started Daily man-db regeneration.
...
$ ls /usr/lib/systemd/system/*.timer
/usr/lib/systemd/system/archlinux-keyring-wkd-sync.timer /usr/lib/systemd/system/paccache.timer
/usr/lib/systemd/system/btrfs-scrub@.timer /usr/lib/systemd/system/pacman-filesdb-refresh.timer
/usr/lib/systemd/system/e2scrub_all.timer /usr/lib/systemd/system/shadow.timer
/usr/lib/systemd/system/fstrim.timer /usr/lib/systemd/system/systemd-sysupdate-reboot.timer
/usr/lib/systemd/system/man-db.timer /usr/lib/systemd/system/systemd-sysupdate.timer
/usr/lib/systemd/system/mdadm-last-resort@.timer /usr/lib/systemd/system/systemd-tmpfiles-clean.timer
/usr/lib/systemd/system/mdcheck_continue.timer /usr/lib/systemd/system/updatedb.timer
/usr/lib/systemd/system/mdcheck_start.timer /usr/lib/systemd/system/xfs_scrub_all.timer
/usr/lib/systemd/system/mdmonitor-oneshot.timer
¶2. Create a timer using free
- /etc/systemd/system/myMonitor.service
# This service unit is for testing timer units
# By David Both
# Licensed under GPL V2
#
[Unit]
Description=Logs system statistics to the systemd journal
Wants=myMonitor.timer
[Service]
Type=oneshot
ExecStart=/usr/bin/free
[Install]
WantedBy=multi-user.target
¶3. Testing
$ sudo nano /etc/systemd/system/myMonitor.service
$ sudo systemctl daemon-reload
$ sudo systemctl start myMonitor
$ sudo systemctl status myMonitor
○ myMonitor.service - Logs system statistics to the systemd journal
Loaded: loaded (/etc/systemd/system/myMonitor.service; disabled; preset: disabled)
Active: inactive (dead)
8月 19 09:07:50 orgpi5Arch systemd[1]: Starting Logs system statistics to the systemd journal...
8月 19 09:07:50 orgpi5Arch free[12656]: total used free shared buff/cache available
8月 19 09:07:50 orgpi5Arch free[12656]: Mem: 16096268 1493116 12660964 91556 1942188 14351300
8月 19 09:07:50 orgpi5Arch free[12656]: Swap: 40921612 0 40921612
8月 19 09:07:50 orgpi5Arch systemd[1]: myMonitor.service: Deactivated successfully.
8月 19 09:07:50 orgpi5Arch systemd[1]: Finished Logs system statistics to the systemd journal.
$ journalctl -S today -u myMonitor.service // -S since today
8月 19 09:07:50 orgpi5Arch systemd[1]: Starting Logs system statistics to the systemd journal...
8月 19 09:07:50 orgpi5Arch free[12656]: total used free shared buff/cache available
8月 19 09:07:50 orgpi5Arch free[12656]: Mem: 16096268 1493116 12660964 91556 1942188 14351300
8月 19 09:07:50 orgpi5Arch free[12656]: Swap: 40921612 0 40921612
8月 19 09:07:50 orgpi5Arch systemd[1]: myMonitor.service: Deactivated successfully.
8月 19 09:07:50 orgpi5Arch systemd[1]: Finished Logs system statistics to the systemd journal.
¶4. write a /etc/systemd/system/myTimer.timer so that it will be triggled every minute!
# This timer unit is for testing
# By David Both
# Licensed under GPL V2
#
[Unit]
Description=Logs some system statistics to the systemd journal
Requires=myMonitor.service
[Timer]
Unit=myMonitor.service
OnCalendar=*-*-* *:*:00
[Install]
WantedBy=timers.target
$ sudo systemctl daemon-reload
$ sudo systemctl start myMonitor
$ ls /etc/systemd/system/*.timer
/etc/systemd/system/myMonitor.timer
¶By chatGPT
- In order to use systemd timer, you have to have a *.service and then a matching name.timer file
Yes, you're on the right track! In systemd, timers are used to schedule and control the execution of tasks at specific intervals or times. Timers are managed through timer units and usually consist of two related files: a .timer unit file and a corresponding .service unit file.
Here's how the setup typically works:
Service Unit (example.service): This is the actual unit that defines the task or service you want to run. It contains information about what should be executed, how it should be executed, and any dependencies it might have. This is the unit that the timer triggers to start.
- Example example.service file:
[Unit]
Description=Example Service
[Service]
ExecStart=/path/to/your/script_or_executable
# Additional settings...
Timer Unit (example.timer): This unit defines when and how often the associated service should be triggered. It specifies the schedule, intervals, and other timing-related settings.
- Example example.timer file:
[Unit]
Description=Example Timer
[Timer]
OnBootSec=15min
OnUnitActiveSec=1h
Unit=example.service
[Install]
WantedBy=timers.target
-
In this example, the timer will start the example.service unit 15 minutes after booting and then repeat every hour.
-
Enabling and Starting: After creating these unit files, you should enable and start the timer unit. This can be done with the following commands:
systemctl enable example.timer
systemctl start example.timer
- The enable command makes the timer unit start automatically on boot, and the start command initiates the timer.
- Remember that the .timer and .service unit filenames must match. For instance, if you have example.timer, the associated service unit should be named example.service.
After setting up the timer, you can manage it using standard systemd commands like systemctl start, systemctl stop, systemctl status, and so on. This allows you to schedule tasks and services in a systematic and controlled manner.