Rich0's Gentoo Blog

Running cron jobs as units automatically

with 9 comments

I just added sys-process/systemd-cron to the Gentoo repository.  Until now I’ve been running it from my overlay and getting it into the tree was overdue.  I’ve found it to be an incredibly useful tool.

All it does is install a set of unit files and a crontab generator.  The unit files (best used by starting/enabling cron.target) will run jobs from /etc/cron.* at the appropriate times.  The generator can parse /etc/crontab and create timer units for every line dynamically.

Note that the default Gentoo install runs the /etc/cron.* jobs from /etc/crontab, so if you aren’t careful you might end up running them twice.  The simplest solutions this are to either remove those lines from /etc/crontab, or install systemd-cron using USE=etc-crontab-systemd which will have the generator ignore /etc/crontab and instead look for /etc/crontab-systemd where you can install jobs you’d like to run using systemd.

The generator works like you’d expect it to – if you edit the crontab file the units will automatically be created/destroyed dynamically.

One warning about timer units compared to cron jobs is that the jobs are run as services, which means that when the main process dies all its children will be killed.  If you have anything in /etc/cron.* which forks you’ll need to have the main script wait at the end.

On the topic of race conditions, each cron.* directory and each /etc/crontab line will create a separate unit.  Those units will all run in parallel (to the extent that one is still running when the next starts), but within a cron.* directory the scripts will run in series.  That may be a bit different from some cron implementations which may limit the number of simultaneous jobs globally.

All the usual timer unit logic applies.  stdout goes to the journal, systemctl list-timers shows what is scheduled, etc.

Written by rich0

July 18, 2015 at 12:00 pm

Posted in gentoo, linux, systemd

9 Responses

Subscribe to comments with RSS.

  1. You should tell the Sabayon developers about this. They are still using vixie-cron, with systemd, which is a strange combination.

    Andrew Miller

    July 20, 2015 at 6:05 pm

    • Unless they just want to use Gentoo ebuilds without modification they’d probably do better to just set up pure timer units. I really see this as a solution for distros that want to support both cron and systemd.

      Distros that intend to primarily support systemd should be using native units all around.

      I’m also not a big fan of the Debian approach of putting wrappers around traditional shell script service files to run them under systemd unless it is just a short-term transition. It makes far more sense to create a shell script that parses unit files and using that as a wrapper around units to provide support back to sysvinit users.

      rich0

      August 1, 2015 at 7:28 am

  2. How about parsing per-user crontabs (/var/spool/cron/crontabs/) ?

    Paul

    August 1, 2015 at 2:45 am

    • I don’t think it does, but I really see this as a solution for distros that want to support both worlds. I’ve yet to see a distro that puts stuff in per-user crontabs.

      I’m pretty sure users can just set up their own timer units.

      rich0

      August 1, 2015 at 7:21 am

      • As an example, I use dcron on Gentoo, it ignores system crontab (/etc/crontab).
        Equivalent file is root crontab (/var/spool/cron/crontabs/root).
        I can edit my tasks in System Settings (KDE).
        I thought at first you were a developer of this tool, so you could add another feature. My mistake 🙂

        Paul

        August 3, 2015 at 5:17 pm

      • Why is this package not covered by virtual/cron, by the way?

        Paul

        August 3, 2015 at 5:25 pm

        • Oversight on my part which is on my todo list, along with the distfiles filename.

          rich0

          August 3, 2015 at 5:42 pm

    • Indeed, that works fine. It is now in the main Gentoo repository. Many thanks go out to those who have been maintaining it on the side so far.

      rich0

      August 7, 2015 at 6:40 pm


Leave a comment