CertGrid CertGrid
Configuration·Python Automation for IT

Python Scripts with cron and systemd Timers

"It works when I run it and not from cron" is almost always the environment. This guide runs one script three ways and counts what it gets: 16 environment variables interactively, 6 under cron, 22 under systemd - and shows cron using your home directory as the working directory while systemd honours WorkingDirectory=. Then the part cron cannot do: a recorded failure you can query afterwards.

OS and Process Automation Guide 23 of 39 Intermediate

systemd user timers need a logged-in session or loginctl enable-linger. OnUnitActiveSec and Persistent= are long-standing options; systemd-analyze verify will check a unit before you install it.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB

This guide includes

Use this when it works by hand and not from cron. This matters because the difference is almost always the environment the script was handed - and one script run three ways shows exactly what each caller gives it.

Before you start

  1. One script, run three ways

  2. A crontab entry, in full

  3. What cron gave it

  4. The same job as a systemd timer

  5. Start it, and what systemd gave it

  6. What the journal recorded

  7. A failing run, and what systemd says about it

  8. The whole comparison, in one file

Official sources