Systemd: Cron Style

Intro

In the spirit of learning more systemd, I’ve followed this description of setting up systemd as a cron replacement for the hourly, daily, weekly, monthly jobs

cd /etc/systemd/system
mkdir ./timer-{hourly,daily,weekly,monthly}.timers
wget https://blog.higgsboson.tk/downloads/timers.tar
tar -xzf timers.tar
rm timers.tar
# add monthly timer and target
# http://www.freedesktop.org/software/systemd/man/systemd.time.html

Lets take a look at each unit file.

declare -a timers=("hourly" "daily" "weekly" "monthly")
for timer in "${timers[@]}"; do
  for unit in timer target; do
    echo "${timer} ${unit}"
    cat -b "/etc/systemd/system/timer-${timer}.${unit}"
    echo ""
  done
done

hourly timer
     1  [Unit]
     2  Description=Hourly Timer

     3  [Timer]
     4  OnBootSec=5min
     5  OnUnitActiveSec=1h
     6  Unit=timer-hourly.target

     7  [Install]
     8  WantedBy=basic.target

hourly target
     1  [Unit]
     2  Description=Hourly Timer Target
     3  StopWhenUnneeded=yes

daily timer
     1  [Unit]
     2  Description=Daily Timer

     3  [Timer]
     4  OnBootSec=10min
     5  OnUnitActiveSec=1d
     6  Unit=timer-daily.target

     7  [Install]
     8  WantedBy=basic.target

daily target
     1  [Unit]
     2  Description=Daily Timer Target
     3  StopWhenUnneeded=yes

weekly timer
     1  [Unit]
     2  Description=Weekly Timer

     3  [Timer]
     4  OnBootSec=15min
     5  OnUnitActiveSec=1w
     6  Unit=timer-weekly.target

     7  [Install]
     8  WantedBy=basic.target

weekly target
     1  [Unit]
     2  Description=Weekly Timer Target
     3  StopWhenUnneeded=yes

monthly timer
     1  [Unit]
     2  Description=Monthly Timer

     3  [Timer]
     4  OnBootSec=20min
     5  OnUnitActiveSec=1month
     6  Unit=timer-monthly.target

     7  [Install]
     8  WantedBy=basic.target

monthly target
     1  [Unit]
     2  Description=Montly Timer Target
     3  StopWhenUnneeded=yes

Enable each of them, e.g.

systemctl enable timer-hourly.timer
systemctl enable timer-daily.timer
systemctl enable timer-weekly.timer
systemctl enable timer-monthly.timer

From there, create service files in the respective timer-{frequency} directory to run them at the frequency.

After a reboot and reloading the daemon (or starting the timers) the timers are active

systemctl list-timers -all

Clean paccache

I’ve posted about this before, but It makes sense to run this as a timer as well.

Here’s the monthly service file I’m using for this

cat /etc/systemd/system/monthly.target.wants/paccache.service
[Unit]
Description=Cleans pacman cache
OnFailure=status-email-jotham@%i.service

[Service]
Type=oneshot
ExecStart=/usr/bin/paccache -ruk1

Here’s an example of viewing the logs for this unit.

journalctl -u paccache.service
-- Logs begin at Thu 2014-05-01 18:12:59 EDT, end at Sun 2015-04-26 19:39:22 EDT. --
Apr 26 19:15:41 archdesktop paccache[11364]: ==> no candidate packages found for pruning

Update the pacman mirrorlist

cat /etc/systemd/system/timer-weekly.target.wants/mirrorlist-update.service
[Unit]
Description=Update pacman mirrorlist
OnFailure=status-email-jotham@%i.service

[Service]
Type=oneshot
ExecStart=/usr/bin/reflector --country 'United States' --country 'Canada' -l 10 -p http --sort rate --save /etc/pacman.d/mirrorlist

List of installed packages

It’s easy enough to mail yourself a list.

pacman -Qqen | mail -s "$HOSTNAME package list" jothamapaloo@gmail.com

I’d like to just throw that in a service file, but it doesn’t acccept operators like pipes and redirections, so it’ll be a one-liner in my bin.

Now I need to repeat all this on my laptop. Oh well, the 2nd time around is always much faster.

Go Top
comments powered by Disqus