§2023-08-19

¶ 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

# 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

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.

[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.

[Unit]
Description=Example Timer

[Timer]
OnBootSec=15min
OnUnitActiveSec=1h
Unit=example.service

[Install]
WantedBy=timers.target
systemctl enable example.timer
systemctl start example.timer

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.