pages tagged obnamyakkinghttp://yakking.branchable.com/tags/obnam/yakkingikiwiki2015-11-02T20:42:10ZSystemd 7 - cron, at and systemd timershttp://yakking.branchable.com/posts/systemd-7-crontab-and-timers/Richard Maw2015-11-02T20:42:10Z2015-10-28T12:00:07Z
<p>You will, from time to time,
feel the need to run a command at a specific time.</p>
<p><a href="http://linux.die.net/man/1/at">at(1)</a> will run a command at a specified time,
so if you need a reminder that you should go to bed at midnight,
you could run the following command:</p>
<pre><code>$ at midnight
at> echo Go to bed | wall ^D
</code></pre>
<p>If you instead need to run a command at regular times,
such as a <a href="http://liw.fi/">backup</a> job,
then you can add a persistent timed job with <a href="http://man7.org/linux/man-pages/man1/crontab.1.html">crontab(1)</a>.</p>
<p>You would run <code>crontab -e</code> to add a command to your crontab,
as described in <a href="http://man7.org/linux/man-pages/man5/crontab.5.html">crontab(5)</a>.</p>
<p>To run nightly <a href="http://obnam.org/">obnam</a> backup
(instructions taken from <a href="http://bastian.rieck.ru/blog/posts/2013/backups_with_obnam/">Bastian Rieck's blog</a>),
add the following to the crontab file.</p>
<pre><code>0 20 * * * /usr/bin/obnam backup $HOME
</code></pre>
<p>This could instead be written as a systemd timer unit,
which can be used without a separate cron service running,
by creating two configuration files as follows:</p>
<pre><code>$ mkdir -p ~/.config/systemd/user
$ cat >~/.config/systemd/user/backup.timer <<'EOF'
> [Unit]
> Description=Backup timer
> [Timer]
> OnCalendar=daily
> [Install]
> WantedBy=default.target
> EOF
$ cat >~/.config/systemd/user/backup.service <<'EOF'
> [Unit]
> Description=Backup Service
> [Service]
> Type=simple
> ExecStart=/usr/bin/obnam backup %h
> [Install]
> WantedBy=default.target
> EOF
$ systemctl --user daemon-reload
$ systemctl --user enable backup.timer backup.service
$ systemctl --user start backup.timer backup.service
</code></pre>
<p>This is more verbose than the cron syntax, though arguably less arcane.</p>
<p>However systemd timer units have the advantage
of allowing you to set <code>WakeSystem=</code>,
which will unsuspend your system to react to timer events.</p>
<p>There are instructions on how to use this to make an alarm clock
on <a href="http://joeyh.name/blog/entry/a_programmable_alarm_clock_using_systemd/">Joey Hess' blog</a>.</p>