Systemd timers instead of cron
-
As I'm sure most of you know, systemd has other units than just services. I'll be dealing with services, timers, and targets in this write up.
The main reason for me switching was ansible-pull. Now it's mostly my fault because I didn't create a script with any logic and just assumed that ansible-pull was smart enough to do this (it's not the main function of Ansible so it's not surprising it doesn't). I was using Ansible pull for basic bare configs after kickstarting and then having it run every 10 minutes to check for changes. I set up a cronjob that called
ansible-pull
to checkout the repo and pull in any changes and run the playbook locally. Well what I didn't realize is that if there is any hangup in that process, ansible-pull isn't killed automatically and then when 10 minutes is up, it will start another ansible-pull process. So after 24 hours of that, some machines had a load of 8+. They were hanging during setup because they were having issues contacting a non-important NFS share that was automounted.Systemd timers will not run another instance until the first is completed, so that fixes this. It also lets you run a service one off unlike cron.
First create your files in
/usr/lib/systemd/system
:touch myscript.{target,timer,service} touch myscript2.service
You don't need a target, but if you want to have more than one script run, it makes it easier.
Here's some examples with a snapshot backup I use for some of my VMs at home:
backup-pbx.service:
[Unit] Description=Backup PBX Before=backup-tower.service [Service] Type=simple ExecStart=/usr/local/bin/snapshot PBX pbx-snap /var/backups/VMs/ hda [Install] WantedBy=backup.target
backup-tower.service:
[Unit] Description=Backup Tower Data After=backup-pbx.service [Service] Type=simple ExecStart=/usr/local/bin/snapshot Tower tower-snap /var/backups/VMs/ vdb [Install] WantedBy=backup.target
backup.target:
[Unit] Description=Snapshot based backup for VMs [Install] WantedBy=default.target
backup.timer:
[Unit] Description=Backup VMs [Timer] OnBootSec=10min OnCalendar=Sun *-*-* 00:00:00 Unit=backup.target [Install] WantedBy=multi-user.target
Now just enable each one
systemctl enable backup-tower backup-pbx backup.target backup.timer
And start the timer:
systemctl start backup.timer
Mine is set to run every week on Sunday at midnight.
This allows us to one off run the services also without going into either crontab or a cron file and grabbing the whole command.
systemctl start backup-tower
These are also all logged with journalctl. So if you want to see all of the entries for
backup-tower
just runjournalctl -u backup-tower
Apr 23 13:58:29 kvm.pa.jhbcomputers.com systemd[1]: Starting Backup Tower data... Apr 23 13:58:29 kvm.pa.jhbcomputers.com snapshot[18508]: Domain snapshot tower-snap created Apr 23 13:58:29 kvm.pa.jhbcomputers.com snapshot[18508]: tar: Removing leading `/' from member names Apr 23 13:58:29 kvm.pa.jhbcomputers.com snapshot[18508]: /data/VMs/tower-var.qcow2 Apr 23 14:02:38 kvm.pa.jhbcomputers.com snapshot[18508]: [43B blob data] Apr 23 14:02:38 kvm.pa.jhbcomputers.com snapshot[18508]: Successfully pivoted Apr 23 14:02:38 kvm.pa.jhbcomputers.com snapshot[18508]: Domain snapshot tower-snap deleted
-
@stacksofplates said in Systemd timers instead of cron:
Now just enable each one
systemctl enable backup-tower backup-pbx backup.target backup.timer
I'm not sure how this works.
Why wouldn't you just enable the backup.timer? Why add everything else in there?
I'm not clear on how it's all linked. -
@obsolesce said in Systemd timers instead of cron:
@stacksofplates said in Systemd timers instead of cron:
Now just enable each one
systemctl enable backup-tower backup-pbx backup.target backup.timer
I'm not sure how this works.
Why wouldn't you just enable the backup.timer? Why add everything else in there?
I'm not clear on how it's all linked.Yeah you don't. I honestly don't remember why I had it this way. Could have just been an accident. Was quite a while ago.