├── README.md └── systemd-timers-jan30-2022.md /README.md: -------------------------------------------------------------------------------- 1 | systemd-timers-jan30-2022.md -------------------------------------------------------------------------------- /systemd-timers-jan30-2022.md: -------------------------------------------------------------------------------- 1 | # Systemd Timers 2 | 3 | 4 | ## Basic problem 5 | 6 | Cron is the 'good old standard' for periodic task management in Unix. 7 | 8 | However, it is FAR from standard, can behave differently (or be configured differently) depending on service/config file/user. 9 | 10 | How do you make a cron job? That depends on: cron, anacron, fcron, jobber, etc. My point is: this is not a steady, unified thing the way some Posix things or Unix traditions are, and it can be annoying depending on how complex your setup grows to be (logging, etc.). 11 | 12 | So now I GENERALLY find myself using systemd timers, with good results. 13 | 14 | 15 | ### Benefits: 16 | 17 | They're systemd units, so they can do everything other systemd units can: 18 | - have dependencies (other units) 19 | - fine granularity for triggering (hw state changes, other events systemd lets you react to) 20 | - easy management via systemctl enable/disable/start/stop 21 | - Timer events can be scheduled based on finish times of executions some delays can be set between executions. 22 | - OnFailure 23 | - EnvironmentFile and other systemd unit goodies 24 | - systemcall filters 25 | - user/group ids 26 | - membershipcontrols 27 | - nice value 28 | - OOM score 29 | - IO scheduling class and priority 30 | - CPU scheduling policy CPU 31 | - affinity umask 32 | - timer slacks 33 | - secure bits 34 | - network access 35 | - etc. etc. etc. 36 | 37 | 38 | ### Downsides 39 | 40 | - can get a bit messy, lots of custom files instead of text in a single file. 41 | 42 | 43 | ## Basic structure 44 | 45 | Cron job line consists of: 46 | 1. scheduling information 47 | 2. action information. 48 | 49 | Systemd timers work the same way, but split each concept into its own file. 50 | 2 unit files: a service and a timer. One benefit you'll notice right away is that this lets us start/test these service units independently of their timers 51 | 52 | ### Create demo service 53 | 54 | `sudo vim /usr/local/bin/tutorialinux-systemd-timers.sh` 55 | 56 | ``` 57 | #!/usr/bin/env bash 58 | echo "I just ran on $(date)" 59 | exit 0 60 | ``` 61 | 62 | Make it executable: 63 | `sudo chmod +x /usr/local/bin/tutorialinux-systemd-timers.sh` 64 | 65 | 66 | ### Create systemd service unit file 67 | 68 | `sudo vim /etc/systemd/system/tutorialinux.service` 69 | 70 | 71 | ``` 72 | [Unit] 73 | Description=Run the tutorialinux echo service 74 | 75 | [Service] 76 | ExecStart=/usr/local/bin/tutorialinux-systemd-timers.sh 77 | ``` 78 | 79 | 80 | ### Create systemd timer unit file 81 | 82 | `sudo vim /etc/systemd/system/tutorialinux.timer` 83 | 84 | ``` 85 | [Unit] 86 | Description=tutorialinux service timer 87 | 88 | [Timer] 89 | OnBootSec=0min 90 | OnCalendar=*:*:0/30 91 | Unit=tutorialinux.service 92 | 93 | [Install] 94 | WantedBy=multi-user.target 95 | 96 | ``` 97 | 98 | ## Cool features: testing 99 | 100 | Test your time formats without saving/re-running your cronjob a bunch of times. 101 | 102 | `systemd-analyze calendar "*:*:0/30"` 103 | 104 | Based on: `man systemd.time` 105 | 106 | 107 | ### Turn it on! 108 | 109 | Tell systemd about the new unit files, enable the timer, and you're good to go! 110 | 111 | `sudo systemctl daemon-reload` 112 | 113 | `sudo systemctl start tutorialinux.timer` 114 | 115 | If you want to keep this around after a reboot: 116 | 117 | `sudo systemctl enable tutorialinux.timer` 118 | 119 | 120 | 121 | 122 | ## Logging 123 | 124 | Just like any other systemd unit -- check the journal! 125 | 126 | `journalctl -fu tutorialinux.service` 127 | 128 | 129 | ## Sources 130 | 131 | - [Official Docs](https://www.freedesktop.org/software/systemd/man/systemd.timer.html) 132 | - [trstringer blog](https://trstringer.com/systemd-timer-vs-cronjob/) 133 | - [Timers vs Cronjobs - stackexchange](https://unix.stackexchange.com/questions/278564/cron-vs-systemd-timers) 134 | 135 | --------------------------------------------------------------------------------