![]() |
|
Setting Up a Timer with systemd in Linux - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Computers (https://sickgaming.net/forum-86.html) +--- Forum: Linux, BSD & Unix (https://sickgaming.net/forum-88.html) +--- Thread: Setting Up a Timer with systemd in Linux (/thread-85763.html) |
Setting Up a Timer with systemd in Linux - xSicKxBot - 07-24-2018 Setting Up a Timer with systemd in Linux ![]() ![]() Previously, we saw how to enable and disable systemd services by hand, at boot time and on power down, when a certain device is activated, and when something changes in the filesystem. Timers add yet another way of starting services, based on… well, time. Although similar to cron jobs, systemd timers are slightly more flexible. Let’s see how they work. “Run when” Let’s expand the Minetest service you set up in the first two articles of this series as our first example on how to use timer units. If you haven’t read those articles yet, you may want to go and give them a look now. So you will “improve” your Minetest set up by creating a timer that will run the game’s server 1 minute after boot up has finished instead of right away. The reason for this could be that, as you want your service to do other stuff, like send emails to the players telling them the game is available, you will want to make sure other services (like the network) are fully up and running before doing anything fancy. Jumping in at the deep end, your minetest.timer unit will look like this: Code: # minetest.timer
[Unit] Description=Runs the minetest.service 1 minute after boot up [Timer] OnBootSec=1 m Unit=minetest.service [Install] WantedBy=basic.targetNot hard at all. As usual, you have a Code: [Unit]Code: [Timer]Code: OnBootSecOther directives you could use are:
Continuing down the minetest.timer unit, the Code: basic.targetThe minetest.service you developed in the first two articles ended up looking like this: Code: # minetest.service
[Unit] Description= Minetest server Documentation= https://wiki.minetest.net/Main_Page [Service] Type= simple User= ExecStart= /usr/games/minetest --server
ExecStartPost= /home//bin/mtsendmail.sh "Ready to rumble?" "Minetest Starting up" TimeoutStopSec= 180 ExecStop= /home//bin/mtsendmail.sh "Off to bed. Nightie night!" "Minetest Stopping in 2 minutes" ExecStop= /bin/sleep 120 ExecStop= /bin/kill -2 $MAINPID [Install] WantedBy= multi-user.targetThere’s nothing you need to change here. But you do have to change mtsendmail.sh (your email sending script) from this: Code: #!/bin/bash # mtsendmail
sleep 20 echo $1 | mutt -F /home/<username>/.muttrc -s "$2" my_minetest@mailing_list.com
sleep 10to this: Code: #!/bin/bash
# mtsendmail.sh
echo $1 | mutt -F /home/paul/.muttrc -s "$2" pbrown@mykolab.comWhat you are doing is stripping out those hacky pauses in the Bash script. Systemd does the waiting now. Making it work To make sure things work, disable minetest.service: Code: sudo systemctl disable minetestso it doesn’t get started when the system starts; and, instead, enable minetest.timer: Code: sudo systemctl enable minetest.timerNow you can reboot you server machine and, when you run Code: sudo journalctl -u minetest.*A Matter of Time A couple of clarifications about why the minetest.timer entry in the systemd’s Journal shows its start time as 09:08:33, while the minetest.service starts at 09:09:18, that is less than a minute later: First, remember we said that the Code: OnBootSec=The other thing is that systemd gives itself a margin of error (by default, 1 minute) to run stuff. This helps distribute the load when several resource-intensive processes are running at the same time: by giving itself a minute, systemd can wait for some processes to power down. This also means that minetest.service will start somewhere between the 1 minute and 2 minute mark after boot is completed, but when exactly within that range is anybody’s guess. For the record, you can change the margin of error with Code: AccuracySec=Another thing you can do is check when all the timers on your system are scheduled to run or the last time the ran: Code: systemctl list-timers --allThe final thing to take into consideration is the format you should use to express the periods of time. Systemd is very flexible in that respect: Code: 2 hCode: 2 hoursCode: 2hrCode: secondsCode: secondCode: secCode: sCode: minutesCode: minuteCode: minCode: mCode: man systemd.timeNext Time You’ll see how to use calendar dates and times to run services at regular intervals and how to combine timers and device units to run services at defined point in time after you plug in some hardware. See you then! Learn more about Linux through the free “Introduction to Linux” course from The Linux Foundation and edX. |