Systemd-Timers are a more flexible alternative to cronjobs. For example, Systemd will take care of the dependencies and the logging of your timer whereas with cron you'd have to program it yourself.
In this example, I will use systemd-timers to create a backup of the zabbix PostgreSQL database.
First, create the service that will actually run the task:
[root@zabbix ~]# cat << EOF >> /etc/systemd/system/zabbix-db-backup.service
[Unit]
Description=Create a Backup of the zabbix database
After=network-online.target postgresql.service
[Service]
Type=simple
Nice=19
ExecStart=/usr/bin/sh /root/scripts/zabbix-db-backup.sh
User=root
Group=root
EOF
Now create the timer unit which is responsible for scheduling your service:
[root@zabbix ~]# cat << EOF >> /etc/systemd/system/zabbix-db-backup.timer
[Unit]
Description=Timer for zabbix-db-backup.service
[Timer]
OnCalendar=*-*-* *:45:00
Unit=zabbix-db-backup.service
Persistent=true
[Install]
WantedBy=timers.target
EOF
You can check your time and date syntax using 'systemd-analyze calendar', here's an example:
[root@zabbix ~]# systemd-analyze calendar '*-*-* *:45:00'
Normalized form: *-*-* *:45:00
Next elapse: Thu 2022-07-28 01:45:00 CEST
(in UTC): Wed 2022-07-27 23:45:00 UTC
From now: 25min left
Reload Systemd to make it aware of the new config files:
[root@zabbix ~]# systemctl daemon-reload
Once Systemd is reloaded, test your task to make sure it works:
[root@zabbix ~]# systemctl start zabbix-db-backup.service
As mentioned earlier, Systemd takes care of logging which can be checked using 'journalctl':
[root@zabbix ~]# journalctl -f -u zabbix-db-backup.service
For the final step, enable the timer-unit so that the timer unit will start after
rebooting the server:
[root@zabbix ~]# systemctl enable --now zabbix-db-backup.timer
Feel free to comment and / or suggest a topic.
Comments
Post a Comment