Sick Gaming
Systemd Timers: Three Use Cases - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Computers (https://www.sickgaming.net/forum-86.html)
+--- Forum: Linux, FreeBSD, and Unix types (https://www.sickgaming.net/forum-88.html)
+--- Thread: Systemd Timers: Three Use Cases (/thread-85947.html)



Systemd Timers: Three Use Cases - xSicKxBot - 08-06-2018

Systemd Timers: Three Use Cases

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/08/systemd-timers-three-use-cases.jpg" width="1500" height="729" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/08/systemd-timers-three-use-cases.jpg" class="ff-og-image-inserted" /></div>
<p>In this systemd tutorial series, we have<a href="https://www.linux.com/blog/learn/intro-to-linux/2018/7/setting-timer-systemd-linux"> already talked about systemd timer units to some degree</a>, but, before moving on to the sockets, let’s look at three examples that illustrate how you can best leverage these units.</p>
<h3>Simple <i>cron</i>-like behavior</h3>
<p>This is something I have to do: collect <a href="https://popcon.debian.org/">popcon data from Debian</a> every week, preferably at the same time so I can see how the downloads for certain applications evolve. This is the typical thing you can have a <i>cron</i> job do, but a systemd timer can do it too:</p>
<pre>
# cron-like popcon.timer [Unit] Description= Says when to download and process popcons [Timer] OnCalendar= Thu *-*-* 05:32:07 Unit= popcon.service [Install] WantedBy= basic.target
</pre>
<p>The actual <i>popcon.service</i> runs a regular <i>wget</i> job, so nothing special. What is new in here is the <code>OnCalendar=</code> directive. This is what lets you set a service to run on a certain date at a certain time. In this case, <code>Thu</code> means “<i>run on Thursdays</i>” and the <code>*-*-*</code> means “<i>the exact date, month and year don’t matter</i>“, which translates to “<i>run on Thursday, regardless of the date, month or year</i>“.</p>
<p>Then you have the time you want to run the service. I chose at about 5:30 am CEST, which is when the server is not very busy.</p>
<p>If the server is down and misses the weekly deadline, you can also work an <i>anacron</i>-like functionality into the same timer:</p>
<pre>
# popcon.timer with anacron-like functionality [Unit] Description=Says when to download and process popcons [Timer] Unit=popcon.service OnCalendar=Thu *-*-* 05:32:07
<b>Persistent=true</b> [Install] WantedBy=basic.target
</pre>
<p>When you set the <code>Persistent=</code> directive to true, it tells systemd to run the service immediately after booting if the server was down when it was supposed to run. This means that if the machine was down, say for maintenance, in the early hours of Thursday, as soon as it is booted again, <i>popcon.service</i> will be run immediately and then it will go back to the routine of running the service every Thursday at 5:32 am.</p>
<p>So far, so straightforward.</p>
<h3>Delayed execution</h3>
<p>But let’s kick thing up a notch and “improve” the <a href="https://www.linux.com/blog/intro-to-linux/2018/6/systemd-services-reacting-change">systemd-based surveillance system</a>. Remember that the system started taking pictures the moment you plugged in a camera. Suppose you don’t want pictures of your face while you install the camera. You will want to delay the start up of the picture-taking service by a minute or two so you can plug in the camera and move out of frame.</p>
<p>To do this; first change the Udev rule so it points to a timer:</p>
<pre>
ACTION=="add", SUBSYSTEM=="video4linux", ATTRS{idVendor}=="03f0", ATTRS{idProduct}=="e207", TAG+="systemd", <b>ENV{SYSTEMD_WANTS}="picchanged.timer"</b>, SYMLINK+="mywebcam", MODE="0666"
</pre>
<p>The timer looks like this:</p>
<pre>
# picchanged.timer [Unit] Description= Runs picchanged 1 minute after the camera is plugged in [Timer] OnActiveSec= 1 m
Unit= picchanged.path [Install]
WantedBy= basic.target
</pre>
<p>The Udev rule gets triggered when you plug the camera in and it calls the timer. The timer waits for one minute after it starts (<code>OnActiveSec= 1 m</code>) and then runs <i>picchanged.path</i>, which <a href="https://www.linux.com/blog/learn/intro-to-linux/2018/6/systemd-services-monitoring-files-and-directories">monitors to see if the master image changes</a>. The <i>picchanged.path</i> is also in charge of pulling in the <i>webcam.service</i>, the service that actually takes the picture.</p>
<h3>Start and stop Minetest server at a certain time every day</h3>
<p>In the final example, let’s say you have decided to delegate parenting to systemd. I mean, systemd seems to be already taking over most of your life anyway. Why not embrace the inevitable?</p>
<p>So you have your Minetest service set up for your kids. You also want to give some semblance of caring about their education and upbringing and have them do homework and chores. What you want to do is make sure Minetest is only available for a limited time (say from 5 pm to 7 pm) every evening.</p>
<p>This is different from “<i>starting a service at certain time</i>” in that, writing a timer to start the service at 5 pm is easy…:</p>
<pre>
# minetest.timer [Unit]
Description= Runs the minetest.service at 5pm everyday [Timer]
OnCalendar= *-*-* 17:00:00 Unit= minetest.service [Install]
WantedBy= basic.target
</pre>
<p>… But writing a counterpart timer that shuts down a service at a certain time needs a bigger dose of lateral thinking.</p>
<p>Let’s start with the obvious — the timer:</p>
<pre>
# stopminetest.timer [Unit]
Description= Stops the minetest.service at 7 pm everyday [Timer]
OnCalendar= *-*-* 19:05:00 Unit= stopminetest.service [Install]
WantedBy= basic.target
</pre>
<p>The tricky part is how to tell <i>stopminetest.service</i> to actually, you know, stop the Minetest. There is no way to pass the PID of the Minetest server from <i>minetest.service</i>. and there are no obvious commands in systemd’s unit vocabulary to stop or disable a running service.</p>
<p>The trick is to use systemd’s <code>Conflicts=</code> directive. The <code>Conflicts=</code> directive is similar to systemd’s <code>Wants=</code> directive, in that it does <i>exactly the opposite</i>. If you have <code>Wants=a.service</code> in a unit called <i>b.service</i>, when it starts, <i>b.service</i> will run <i>a.service</i> if it is not running already. Likewise, if you have a line that reads <code>Conflicts= a.service</code> in your <i>b.service</i> unit, as soon as <i>b.service</i> starts, systemd will stop <i>a.service</i>.</p>
<p>This was created for when two services could clash when trying to take control of the same resource simultaneously, say when two services needed to access your printer at the same time. By putting a <code>Conflicts=</code> in your preferred service, you could make sure it would override the least important one.</p>
<p>You are going to use <code>Conflicts=</code> a bit differently, however. You will use <code>Conflicts=</code> to close down cleanly the <i>minetest.service</i>:</p>
<pre>
# stopminetest.service [Unit]
Description= Closes down the Minetest service
Conflicts= minetest.service [Service]
Type= oneshot
ExecStart= /bin/echo "Closing down minetest.service"
</pre>
<p>The <i>stopminetest.service</i> doesn’t do much at all. Indeed, it could do nothing at all, but just because it contins that <code>Conflicts=</code> line in there, when it is started, systemd will close down <i>minetest.service</i>.</p>
<p>There is one last wrinkle in your perfect Minetest set up: What happens if you are late home from work, it is past the time when the server should be up but playtime is not over? The <code>Persistent=</code> directive (see above) that runs a service if it has missed its start time is no good here, because if you switch the server on, say at 11 am, it would start Minetest and that is not what you want. What you really want is a way to make sure that systemd will only start Minetest between the hours of 5 and 7 in the evening:</p>
<pre>
# minetest.timer [Unit]
Description= Runs the minetest.service every minute between the hours of 5pm and 7pm [Timer]
<b>OnCalendar= *-*-* 17..19:*:00</b>
Unit= minetest.service [Install]
WantedBy= basic.target
</pre>
<p>The line <code>OnCalendar= *-*-* 17..19:*:00</code> is interesting for two reasons: (1) <code>17..19</code> is not a point in time, but a period of time, in this case the period of time between the times of 17 and 19; and (2) the <code>*</code> in the minute field indicates that the service must be run every minute. Hence, you would read this as “<i>run the minetest.service every minute between 5 and 7 pm</i>“.</p>
<p>There is still one catch, though: once the <i>minetest.service</i> is up and running, you want <i>minetest.timer</i> to stop trying to run it again and again. You can do that by including a <code>Conflicts=</code> directive into <i>minetest.service</i>:</p>
<pre>
# minetest.service [Unit]
Description= Runs Minetest server
<b>Conflicts= minetest.timer</b> [Service]
Type= simple
User= &lt;<i>your user name</i>&gt; ExecStart= /usr/bin/minetest --server ExecStop= /bin/kill -2 $MAINPID [Install]
WantedBy= multi-user.targe
</pre>
<p>The <code>Conflicts=</code> directive shown above makes sure <i>minetest.timer</i> is stopped as soon as the <i>minetest.service</i> is successfully started.</p>
<p>Now enable and start <i>minetest.timer</i>:</p>
<pre>
systemctl enable minetest.timer
systemctl start minetest.timer
</pre>
<p>And, if you boot the server at, say, 6 o’clock, <i>minetest.timer</i> will start up and, as the time falls between 5 and 7, <i>minetest.timer</i> will try and start <i>minetest.service</i> every minute. But, as soon as <i>minetest.service</i> is running, systemd will stop <i>minetest.timer</i> because it “conflicts” with <i>minetest.service</i>, thus avoiding the timer from trying to start the service over and over when it is already running.</p>
<p>It is a bit counterintuitive that you use the service to kill the timer that started it up in the first place, but it works.</p>
<h3>Conclusion</h3>
<p>You probably think that there are better ways of doing all of the above. I have heard the term “overengineered” in regard to these articles, especially when using systemd timers instead of cron.</p>
<p>But, the purpose of this series of articles is not to provide the best solution to any particular problem. The aim is to show solutions that use systemd units as much as possible, even to a ridiculous length. The aim is to showcase plenty of examples of how the different types of units and the directives they contain can be leveraged. It is up to you, the reader, to find the real practical applications for all of this.</p>
<p>Be that as it may, there is still one more thing to go: next time, we’ll be looking at <i>sockets</i> and <i>targets</i>, and then we’ll be done with systemd units.</p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>