systemd timers for DuckDNS

SneWs's picture

I had to migrate a couple of cron jobs over to systemd timers. This is a short walk through on how I did it for my DuckDNS updater.

Basically, we need to create two files under /etc/systemd/system or /etc/systemd/user dep. on your specific needs.

For DuckDNS I created the config under /etc/systemd/system. So any filenames or paths that are not rooted paths are in relation to /etc/systemd/system, so adjust accordingly for your own needs.

duckdns.timer

[Unit]
Description=DuckDNS update every 5min

[Timer]
OnBootSec=5min
OnCalendar=*:0/5

[Install]
WantedBy=basic.target  

duckdns.service

[Unit]
Description=Updates DuckDNS records

[Service]
Type=oneshot
ExecStart=/usr/bin/sh -c 'echo url="https://www.duckdns.org/update?domains=[YOUR_DOMAIN_NAME_HERE]&token=[YOUR_TOKEN_HERE]&ip=" | curl -k -K -'

Uhm, what is all that jibberjabber...
There are a couple of new things here that are very different compared to how you would configure this in your crontab.

First, you have to have a service defined for a timer, otherwise it just won't work, if you have documentation saying otherwise, please let me know.

Second, the service is defined as a oneshot service, this is important for the DuckDns updater since it should not be running all the time. And the timer will invoke it every time it elapses, and once done there is nothing more for it to do.

Third, the format for the timer config, OnBootSec=5min this ensures that after the system boots, it will wait for 5min before issuing the first run of the timer. OnCalendar=*:0/5 means that we want the timer to be invoked every 5min. For a nice detailed walk through on how timers works under systemd, see https://wiki.archlinux.org/index.php/Systemd/Timers

And finally, you will have to replace [YOUR_DOMAIN_NAME_HERE] and [YOUR_TOKEN_HERE] with your own domain and token from the DuckDNS dashboard.

Once you have created the above files, you will need to start the service part of it to ensure that it works as expected.

Issue:

systemctl start duckdns.service

Validate that your timer has been registered:

systemctl list-timers

You should see some output from the list-timers command similar to this:

NEXT                          LEFT          LAST                          PASSED        UNIT                         >
Wed 2018-08-01 19:10:00 CEST  3min 9s left  Wed 2018-08-01 19:05:04 CEST  1min 46s ago  duckdns.timer

Make sure that your duckdns.timer shows up in the output. If everything looks good, or not, journalctl is your friend to query state and logs for services.

For example, if you want to see the logs for the duckdns.service, issue:

journalctl -u duckdns.service

Make sure that you can see output similar to this:

Jul 31 20:20:02 hawk systemd[1]: Starting Updates DuckDNS records...
Jul 31 20:20:02 hawk sh[3030]:   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Jul 31 20:20:02 hawk sh[3030]:                                  Dload  Upload   Total   Spent    Left  Speed
Jul 31 20:20:04 hawk sh[3030]: [316B blob data]
Jul 31 20:20:04 hawk sh[3030]: OK

Tags: 

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.