Tag: Cron

  • systemd timers vs cron: a cleaner way to run scheduled Linux jobs

    systemd timers vs cron: a cleaner way to run scheduled Linux jobs

    systemd timers are worth another look if your Linux servers already run systemd and your scheduled jobs have grown beyond a one-line cron entry. The argument is not that cron is obsolete. It is that many production tasks need logs, status, retry behavior, missed-run handling, and readable schedules more than they need the shortest possible config file.

    The short version

    • systemd timers split the schedule from the work: a .timer decides when to run, while a .service defines what runs.
    • For operators, the biggest win is observability. systemctl status, journalctl, and systemctl list-timers make failures easier to inspect than a quiet crontab.
    • Timer expressions can be wall-clock based, such as OnCalendar=daily, or event based, such as OnBootSec=1h and OnUnitActiveSec=1h.
    • Options like Persistent=true, RandomizedDelaySec, and WakeSystem help with laptops, fleets, and jobs that should not all fire at the same second.
    • Cron still matters, especially across mixed Unix, BSD, embedded, or older Linux environments where systemd is not guaranteed.

    What happened

    Tyler Langlois published a long, practical defense of systemd timers as a better default for many scheduled Linux jobs. The piece walks through a service-and-timer pair, shows how timer units activate matching service units, and points readers toward systemd.time(7) and systemd-analyze calendar for checking schedule expressions before trusting them in production.

    The useful part is the framing. Cron makes it easy to say “run this at this time.” systemd timers make it easier to say “run this service under the same supervision, logging, environment, and failure semantics I use for the rest of the machine.” That matters for backups, cleanup jobs, refresh tasks, polling loops, and other background work that becomes painful only after it fails.

    If you follow Linux and infrastructure tooling, this fits naturally beside other practical operations notes in the IT & AI archive: small workflow changes that do not look dramatic, but remove a lot of late-night debugging.

    Why this is worth watching

    systemd timers change the shape of a scheduled job. Instead of hiding the command inside a crontab line, you describe the command as a service unit. That means stdout and stderr land in the journal, the job can use systemd features such as ExecCondition=, OnFailure=, and Restart=, and the current state is visible through familiar systemctl commands.

    The schedule language is also less narrow than classic cron. OnCalendar= covers fixed dates and times. OnBootSec= handles jobs that should run after a machine has been up for a while. OnUnitActiveSec= handles “run again one hour after the last successful activation” style tasks. For many jobs, that is closer to the real requirement than “run at minute 0 of every hour.”

    The fleet angle is easy to miss. If every server checks the same API at midnight, cron can create avoidable spikes unless you build jitter yourself. systemd timers include randomized delay options, so the schedule can spread work across machines without turning the command into a pile of shell glue.

    What Hacker News readers are arguing about

    The Hacker News discussion was tiny, so there is no broad community verdict to report. The most useful objection came from a commenter who works across mixed commercial environments: cron is still the portable skill, and good cron setups can explicitly set PATH, redirect output, and feed audit logs or syslog pipelines.

    That is the right caveat. systemd timers are compelling when systemd is already the operating layer. They are a weaker default if you support BSD, embedded Linux, vendor appliances, HPC systems, or older distributions where systemd is absent or politically unwelcome. The practical takeaway is not “replace every crontab.” It is “do not leave production Linux jobs in cron by habit when systemd would give you better inspection tools.”

    systemd timers in practice

    The safest first test is a job with annoying failure modes: a backup, cleanup task, local cache refresh, or polling script that already sends people looking through logs. Those are the jobs where systemd timers usually pay for their extra unit file.

    The practical read

    Use cron for simple, portable, low-risk jobs. Use systemd timers when you care about status, logs, dependency ordering, missed runs, restart behavior, or event-based scheduling.

    A reasonable migration path is boring: pick one recurring job that already causes questions when it fails. Move the command into a .service, create a matching .timer, validate the schedule with systemd-analyze calendar, then check it with systemctl list-timers and journalctl -u your-job.service. If that feels clearer than the old crontab, move the next job.

    For developer tool builders, there is also a product lesson here. Scheduled work is easier to trust when the system can answer three questions quickly: when did it last run, what happened, and when will it run again? systemd timers get closer to that model than a bare cron line.

    Sources