cron2timer
Live tool: https://aamirtechs.github.io/cron2timer/ (runs in your browser, no install)
Convert crontab lines into systemd .service and .timer unit files.
cron2timer is a zero-dependency Python CLI that reads a crontab and generates
one .service plus one .timer per job, with correct OnCalendar=
expressions, environment variable mapping, and sane systemd defaults.
It is built for people migrating cron jobs to systemd on modern Linux
distros, and for anyone who wants their scheduled jobs version-controlled
as unit files.
Install
Requires Python 3.8 or newer. No dependencies beyond the standard library.
pip install cron2timer
Or run it straight from the source checkout:
python3 cron2timer.py mycrontab --out-dir units/
Usage
# Validate a crontab first (exit 0 = ok, 1 = errors, with line numbers)
cron2timer mycrontab --check
# Write units/*.service and units/*.timer
cron2timer mycrontab --out-dir units/
# Print the units to stdout instead of writing files
cron2timer mycrontab --stdout
# Read from stdin
crontab -l | cron2timer --out-dir units/
After generating, copy the units into place and enable them:
sudo cp units/* /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable --now backup-db.timer
User timers
Many cron jobs run as non-root users. Pass --user and install with systemctl --user:
crontab -l | cron2timer --user --out-dir ~/.config/systemd/user
systemctl --user daemon-reload
systemctl --user enable --now backup-db.timer
--user writes @reboot services to default.target instead of multi-user.target.
Example
Given this crontab (sample.cron):
# Nightly database backup, weekdays at 2:30 AM
MAILTO=dba@example.com
30 2 * * 1-5 /usr/local/bin/backup-db.sh --full
# Sync every 15 minutes during business hours
*/15 9-17 * * MON-FRI /opt/app/sync.py
@daily /usr/bin/logrotate-cron.sh
@reboot /opt/app/start-server.sh
cron2timer sample.cron --check prints:
OK: 4 job(s) parsed, no errors
cron2timer sample.cron --out-dir units/ writes
backup-db.service, backup-db.timer, sync.service, sync.timer,
logrotate-cron.service, logrotate-cron.timer, and start-server.service,
and prints:
wrote units/backup-db.service
wrote units/backup-db.timer
wrote units/sync.service
wrote units/sync.timer
wrote units/logrotate-cron.service
wrote units/logrotate-cron.timer
wrote units/start-server.service
note: line 9 is @reboot, service installs to multi-user.target, no timer generated
units/backup-db.timer (real output):
# Generated by cron2timer from crontab line 3
# Original cron line: 30 2 * * 1-5 /usr/local/bin/backup-db.sh --full
[Unit]
Description=Timer for cron job backup-db
[Timer]
OnCalendar=Mon..Fri *-*-* 02:30:00
Unit=backup-db.service
[Install]
WantedBy=timers.target
units/backup-db.service (real output):
# Generated by cron2timer from crontab line 3
# Original cron line: 30 2 * * 1-5 /usr/local/bin/backup-db.sh --full
[Unit]
Description=Cron job converted by cron2timer: /usr/local/bin/backup-db.sh --full
[Service]
Type=oneshot
Environment="MAILTO=dba@example.com"
ExecStart=/bin/sh -c '/usr/local/bin/backup-db.sh --full'
units/sync.timer (real output, note the business-hours range and step):
# Generated by cron2timer from crontab line 6
# Original cron line: */15 9-17 * * MON-FRI /opt/app/sync.py
[Unit]
Description=Timer for cron job sync
[Timer]
OnCalendar=Mon..Fri *-*-* 9..17:0/15:00
Unit=sync.service
[Install]
WantedBy=timers.target
Schedule conversion
| Cron |
systemd OnCalendar= |
*/15 * * * * |
*-*-* *:0/15:00 |
30 2 * * 1-5 |
Mon..Fri *-*-* 02:30:00 |
0 9 * JAN MON |
Mon *-1-* 09:00:00 |
@daily / @midnight |
*-*-* 00:00:00 |
@hourly |
*-*-* *:00:00 |
@weekly |
Sun *-*-* 00:00:00 |
@monthly |
*-*-1 00:00:00 |
@yearly / @annually |
*-1-1 00:00:00 |
Conversion details worth knowing:
- cron
*/n becomes systemd 0/n (or 1/n for day-of-month and month),
because systemd requires an explicit start value for steps.
- cron ranges
9-17 become systemd 9..17.
- Month names (
jan) become numbers; systemd does not accept month names.
- Day-of-week numbers (
0 and 7 both mean Sunday) and names become
systemd weekday names (Sun, Mon..Fri).
- cron steps on day-of-week (
*/2) are expanded to explicit name lists
(Sun,Tue,Thu,Sat), because systemd does not allow steps there.
- When both day-of-month and day-of-week are restricted, cron fires when
either matches (OR) while a single systemd expression requires both
(AND), so cron2timer emits two
OnCalendar= lines, which a timer ORs.
@reboot has no timer equivalent; cron2timer emits only a service that
installs to multi-user.target.
Environment variables
VAR=value assignments in the crontab become Environment= directives in
the generated service. Assignments apply to the jobs below them, matching
cron semantics. Values are re-quoted safely for systemd.
Known limitations
- An unescaped
% in a cron command means newline to cron; cron2timer
passes it through literally and --check prints a warning.
MAILTO is carried over as an Environment= variable; systemd does not
mail job output by itself. Pipe output to a mailer if you need that.
- Commands run via
/bin/sh -c, matching how cron executes them.
Development
python3 -m pytest tests/ -q
The test suite includes a test that validates every generated
OnCalendar= expression with systemd-analyze calendar when it is
available, so the conversions are checked against the real systemd parser.
License
MIT. See LICENSE.