Managing systemd timer units as cron replacements on CentOS
Cron remains familiar to CentOS administrators, but systemd timers offer event-driven scheduling with native dependencies, journal logging and precise calendar expressions.
On RHEL 8/9 and CentOS Stream, a timer is a pair: the .timer defines when to run, while the .service defines what to execute. Both fit the familiar systemctl workflow.
Australian scheduling needs care: Sydney and Melbourne change daylight-saving time, Brisbane does not, Adelaide uses a half-hour offset and Perth uses Western Standard Time. Servers in these cities may serve different business hours, so a job labelled “09:00” should name an explicit Australia/... zone.
Australian administrators often call a small, reversible change “a go”, while hosting mixes managed VPS plans, cPanel/WHM estates and cloud instances. RHEL-compatible images remain common. For adjacent command-line practice, Android Tutorial Point is a useful companion.
Why timers suit CentOS operations
Cron jobs are often shell commands relying on a user's aliases and environment. A systemd timer can call a dedicated executable, run it as a named account and report status in the same journal as other services.
Persistent=true starts a calendar job after downtime when its activation was missed. AccuracySec limits clock jitter, while RandomizedDelaySec can spread simultaneous jobs. Dependencies can also be expressed with After= and Wants=.
Migrate gradually: cron’s shell expansion can hide assumptions that ExecStart exposes. Validate each calendar with systemd-analyze calendar, run the service once, then remove the old entry only after its result is understood.
Build a service and timer pair
Create the dbbackup account, then place site-specific units in /etc/systemd/system, owned by root and protected from group or other writes. This service runs a one-shot backup as that account.
# /etc/systemd/system/db-backup.service
[Unit]
Description=Back up the application database
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
User=dbbackup
ExecStart=/usr/local/sbin/db-backup
The timer names the service, fixes its calendar, and remembers an activation missed while the machine was off. AccuracySec=1min bounds jitter; RandomizedDelaySec=30s adds a small spread.
# /etc/systemd/system/db-backup.timer
[Unit]
Description=Run the database backup daily
[Timer]
OnCalendar=*-*-* 02:00:00 Australia/Sydney
Persistent=true
Unit=db-backup.service
AccuracySec=1min
RandomizedDelaySec=30s
[Install]
WantedBy=timers.target
After systemctl daemon-reload, enable and start the timer with systemctl enable --now db-backup.timer. The service does not need separate enablement. Check systemctl list-timers db-backup.timer and journalctl -u db-backup.service for the next run and its output.
Translate cron schedules accurately
Inventory crontab -l for every account, /etc/crontab and files in /etc/cron.d. Record each command, owner, environment, working directory and expected runtime; cron’s five fields and shell expansion do not map cleanly to OnCalendar.
Use expressions such as OnCalendar=hourly, OnCalendar=daily, or OnCalendar=Mon..Fri 09:00:00 Australia/Sydney. Validate them with systemd-analyze calendar 'Mon..Fri 09:00:00 Australia/Sydney' and inspect the listed events around Sydney’s and Melbourne’s clock changes.
For interval jobs, OnBootSec=10min with OnUnitActiveSec=1h is clearer than a forced clock time. Persistent=true applies to calendar timers, not monotonic intervals. Test output and exit status, then disable the cron entry while retaining a rollback copy.
Operate timers with control and visibility
Use systemctl list-timers --all to include inactive timers and systemctl status <unit> to separate a waiting timer from a failed service. journalctl -u <service> --since today captures stdout, stderr and systemd metadata without separate mail files.
Run privileged tasks narrowly: use absolute paths, a dedicated system user, NoNewPrivileges=true, ProtectSystem=strict and only necessary ReadWritePaths. User timers belong in ~/.config/systemd/user; systemctl --user requires the session to persist, which loginctl enable-linger can arrange.
The Linux commands reference helps check systemctl, journalctl and systemd-analyze options. Australian managed providers often standardise RHEL-compatible images and central logging, while local MSPs may retain cPanel hosts, so test on the exact production image.
Recommendations for a controlled migration
The operational differences are:
| Concern | Cron | Systemd timer | CentOS result |
|---|---|---|---|
| Scheduling | crond polls each minute |
A timer activates a named service | Calendar or interval triggers |
| Missed runs | Usually skipped | Persistent=true can catch up |
Recovery policy is explicit |
| Visibility | Mail or redirected files | Journal and systemctl status |
One diagnostic workflow |
| Context | Shell environment | Explicit service properties | Fewer hidden dependencies |
| User jobs | Per-user crontab | User timer with lingering enabled | Session lifetime is controlled |
A low-risk rollout keeps the old schedule recoverable while each new unit proves itself.
- Inventory all user, system and
/etc/cron.dschedules before changing anything. - Convert one low-impact job and validate its
OnCalendarexpression withsystemd-analyze calendar. - State the intended
Australia/...time zone and review daylight-saving transitions. - Use absolute executable paths, explicit users and minimum filesystem privileges.
- Enable with
systemctl enable --now, then verifylist-timersand journal output. - Retain the cron definition and rollback procedure until repeated runs succeed.
The next concrete step is to inventory crontab -l, /etc/crontab and /etc/cron.d on a non-production CentOS host, recording each schedule, owner, command and expected duration before creating the first timer.