Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

While I agree Systemd timers aren't as ergonomic to set up - there are some benefits that I would basically never give up at this point.

* Running the service on command ( this is the best way to troubleshoot issues with your slightly different environments / cron's version of shell. I've spent many minutes over my career - setting the crontab one minute and the future and waiting for it to run / fail )

* Easily inspecting when the next run of the service is. ( list-timers )

* Straightforward access to the logs ( Always journalctl )

* Specifying environment variables easily

* OnFailure



* Built in locking (so you don't need to worry about two instances of your cron running at the same time)

* Built in functionality for telling when the cron last ran, and whether it was successful or not (so you can do alerting)

* Ability to specify human-readable schedules

* Built-in feature for randomising cron start time to avoid stampeding


The built-in locking is IMO a big deal.

If you have a script that you run every 24 hours, what happens when it takes 25 hours to complete?

(My experience is that this happens only years later, and the people who wrote the cronjob are gone.)


Speaking on OnFailure, one of the unfortunate aspects of systemd timers (which are otherwise quite nice) is you need to do extra work to get proper emails when something fails. Here's how I do this:

Define email-unit-status@.service

    [Unit]
    Description=Email status for %i to root
    After=network.target

    [Service]
    Type=oneshot
    ExecStart=email-unit-status %i

Define email-unit-status (a script)

    #!/usr/bin/env bash
    set -eu
     
    dest="admin@whatever.com"
     
    userflag=
    if [[ $HOME == /home/* ]]; then
      userflag=--user
    fi
     
    html=$(SYSTEMD_COLORS=1 systemctl $userflag status --full "$1" | ansi2html -w -c)
     
    /usr/sbin/sendmail -t <<ERRMAIL
    To: $dest
    From: systemd <${USER}@${HOSTNAME}>
    Subject: SystemD unit failure alert: $1
    Content-Transfer-Encoding: 8bit
    Content-Type: text/html; charset=UTF-8
    Mime-Version: 1.0
     
    $html
    ERRMAIL
Then you can add to a service:

    OnFailure=email-unit-status@%n
You will need to install the ansi2html tool. This stuff should come with systemd really.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: