Skip to content

Systemd Timers vs Cron: Scheduling Linux Jobs

Linux servers depend on scheduled tasks for database backups, log rotation, cleanup scripts, certificate checks, application maintenance, and routine system administration. Two common ways to schedule these jobs are cron and systemd timers.

Choosing between them affects more than scheduling syntax. It also determines how jobs are managed, where execution logs are collected, how missed runs are handled, and how scheduled work interacts with other system services.

In this guide, we compare systemd timers vs cron, explain their practical differences, and walk through working examples you can use on a Linux VPS or dedicated server.

What Is Cron?

Cron is a time-based job scheduler available on many Unix-like operating systems. It runs commands according to schedules defined in crontab files.

Cron is widely used because its configuration is simple and portable across many Linux distributions. An administrator can schedule a script to run every five minutes, every night, or on a specific day of the month without creating a separate service definition.

For example, this crontab entry runs a backup script every day at 2:00 AM:

0 2 * * * /usr/local/sbin/daily-backup.sh

Edit the current user’s crontab with:

crontab -e

To inspect the configured jobs, run:

crontab -l

System-wide schedules may also be defined in /etc/crontab, /etc/cron.d/, and distribution-specific directories such as /etc/cron.daily/.

Cron is a practical choice for straightforward scheduled commands, scripts, and maintenance tasks that do not need extensive integration with the service manager.

What Are Systemd Timers?

Systemd timers are scheduling units managed by systemd. Instead of placing a command directly into a schedule, you normally create a .timer unit and pair it with a .service unit that defines the work to execute.

For example, daily-backup.timer schedules daily-backup.service. The service describes the command, execution environment, user, dependencies, and other settings.

This separation provides several advantages:

  • Scheduled tasks can be inspected through systemctl.
  • Execution logs can be reviewed through the system journal.
  • Service dependencies and execution ordering can be configured.
  • Calendar-based and elapsed-time schedules are supported.
  • Persistent calendar timers can catch up on certain missed runs after the timer becomes active again.
  • Service-level controls can help manage execution and resource usage.

Systemd timers are particularly useful when scheduled jobs are part of a server’s operational workflow rather than isolated commands.

Systemd Timers vs Cron: Key Differences

Feature Cron Systemd timers
Configuration Crontab entries Separate .timer and .service units
Schedule format Five-field time expression in common crontabs Calendar expressions and monotonic time options
Missed runs Traditional cron does not automatically replay every missed occurrence Persistent=true can catch up a missed calendar activation
Logging Often syslog or mail, depending on configuration Usually available through the system journal
Dependencies Usually handled inside scripts or wrappers Supports systemd unit dependencies and ordering
Execution environment Relatively minimal and implementation-dependent Service settings can define users, directories, environment, and restrictions
Runtime management Managed by the cron daemon Managed through systemd tools
Portability Broad support across Unix-like systems Requires systemd
Best fit Simple, portable scheduled commands Jobs that benefit from service management and system integration

Neither option guarantees that a job will finish successfully. A scheduler can start a command, but the script can still fail because of missing files, unavailable storage, permissions, network problems, or application errors.

1. Scheduling Jobs with Cron

Cron uses a five-field schedule in the usual format:

minute hour day-of-month month day-of-week

For example:

# Run every day at 2:00 AM
0 2 * * * /usr/local/sbin/daily-backup.sh

# Run every five minutes
*/5 * * * * /usr/local/sbin/check-service.sh

# Run every Sunday at 3:30 AM
30 3 * * 0 /usr/local/sbin/weekly-maintenance.sh

# Run on the first day of each month at midnight
0 0 1 * * /usr/local/sbin/monthly-report.sh

These entries can be added to the appropriate user’s crontab.

Use absolute paths

Cron jobs usually run with a smaller environment than an interactive shell. Do not assume that the job will inherit the same PATH, working directory, shell variables, or configuration as your SSH session.

Prefer:

0 2 * * * /usr/bin/python3 /opt/maintenance/cleanup.py

Instead of relying on:

0 2 * * * python3 cleanup.py

The second example may fail if the executable or script cannot be found in the job’s environment.

For important jobs, also define the required environment variables explicitly or load them safely from a controlled configuration file.

Capture cron output

Cron implementations and system configurations differ in how they handle job output. Depending on the setup, output may be mailed to a local account or recorded through system logging.

You can redirect standard output and errors to a dedicated log:

0 2 * * * /usr/local/sbin/daily-backup.sh >> /var/log/daily-backup.log 2>&1

Make sure the selected user can write to the log file and that log rotation is configured. Otherwise, the log can grow indefinitely.

For a production backup job, logging is useful, but you should also monitor its exit status and verify that the backup can actually be restored.

2. Creating a Systemd Timer

Consider a server where a maintenance script must run every day at 2:00 AM. We can create a service unit and a corresponding timer unit.

The following example assumes a systemd-based Linux distribution and a script at /usr/local/sbin/daily-backup.sh.

Step 1: Create the service unit

Create the file:

sudo nano /etc/systemd/system/daily-backup.service

Add the following configuration:

[Unit]
Description=Daily server backup

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/daily-backup.sh

This defines a one-shot service that runs the script and exits when the command finishes.

Before enabling it, confirm that the script exists, has appropriate permissions, and handles errors correctly:

sudo test -x /usr/local/sbin/daily-backup.sh
echo $?

An exit code of 0 from the test means the file exists and is executable. It does not guarantee that the backup itself will succeed.

You can test the service manually:

sudo systemctl start daily-backup.service
sudo systemctl status daily-backup.service

Step 2: Create the timer unit

Create the timer file:

sudo nano /etc/systemd/system/daily-backup.timer

Add:

[Unit]
Description=Run daily server backup at 2 AM

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
Unit=daily-backup.service

[Install]
WantedBy=timers.target

Here is what the main directives mean:

  • OnCalendar=*-*-* 02:00:00 schedules the job for 2:00 AM every day according to the system’s calendar time.
  • Persistent=true records the last trigger time and can trigger the service after the timer is reactivated if a calendar run was missed while the timer was inactive.
  • Unit=daily-backup.service identifies the service to start.
  • WantedBy=timers.target allows the timer to be enabled as part of the normal timer target.

The Persistent setting applies to calendar-based timers. It is not a general-purpose mechanism for replaying every missed execution of every timer type.

Step 3: Reload systemd and enable the timer

After creating the unit files, run:

sudo systemctl daemon-reload
sudo systemctl enable --now daily-backup.timer

Check its status:

sudo systemctl status daily-backup.timer

List active timers and their next scheduled execution:

systemctl list-timers --all

To inspect the last execution of the backup service:

sudo systemctl status daily-backup.service

Once configured, systemd manages the schedule and activates the service when the timer elapses.

3. Missed Runs: What Happens When a Server Is Offline?

Missed executions are one of the most important differences between systemd timers and traditional cron jobs.

Imagine a backup is scheduled for 2:00 AM, but the VPS is powered off from 1:00 AM until 5:00 AM.

What happens with cron?

Traditional cron does not normally replay every scheduled occurrence that passed while its daemon or host was unavailable. When the machine starts again, the job waits for its next scheduled time.

For example, a daily 2:00 AM backup may not run until 2:00 AM the following day.

There are exceptions and alternative implementations. Tools such as Anacron are designed to handle certain periodic jobs that may be missed while a machine is offline, and some cron implementations provide additional scheduling behavior.

What happens with systemd timers?

For a calendar timer configured with:

Persistent=true

systemd stores a timestamp of the last trigger. When the timer becomes active again, it can start the associated service if at least one scheduled calendar event was missed while the timer was inactive.

This is useful for jobs such as periodic maintenance or daily backup attempts where running once after an outage is preferable to silently waiting for the next scheduled day.

However, there are important limitations:

  • Persistent timers do not replay every missed occurrence individually. Multiple missed calendar events can result in a single activation.
  • The timer must become active again for the catch-up behavior to apply.
  • The service may still fail when it runs.
  • This setting does not provide a durable queue of all missed jobs or guarantee exactly-once execution.

For business-critical tasks, use application-level tracking, monitoring, and recovery logic if every scheduled operation must be accounted for.

Choose the missed-run behavior deliberately

Use Persistent=true when a missed calendar activation should trigger one catch-up run.

Do not enable catch-up behavior without considering the workload. If a job processes financial records, generates time-sensitive reports, or performs incremental backups, blindly rerunning an operation may be insufficient or unsafe. The script should know which work has already completed and how to resume without duplicating actions.

4. Calendar Schedules vs Elapsed-Time Schedules

Cron primarily expresses schedules in calendar fields. Systemd timers can schedule work using calendar expressions or relative time intervals.

Calendar-based scheduling

Use OnCalendar= when the job must run at a particular time of day, on a particular weekday, or according to another calendar pattern.

For example:

[Timer]
OnCalendar=Mon..Fri 08:30:00

This schedules the service for 8:30 AM Monday through Friday.

Another example:

[Timer]
OnCalendar=*-*-* 00,12:00:00

This schedules a run at midnight and noon every day.

You can validate calendar expressions before deploying a timer:

systemd-analyze calendar 'Mon..Fri 08:30:00'

This command helps confirm how systemd interprets the expression and displays upcoming occurrences.

Elapsed-time scheduling

Use monotonic timer directives when the schedule should be relative to startup, timer activation, or a service’s activation and completion.

For example:

[Timer]
OnBootSec=5min
OnUnitActiveSec=1h

This schedules an initial activation five minutes after boot, then subsequent activations relative to the last activation of the associated unit.

Another useful pattern is:

[Timer]
OnBootSec=10min
OnUnitInactiveSec=30min

This sets an initial delay after boot and schedules subsequent activations relative to when the associated service becomes inactive.

These directives are not interchangeable with a fixed daily clock time. The distinction matters for polling tasks, periodic checks, and workloads that should be scheduled relative to service execution.

Also remember that timers are not necessarily executed at the exact nominal instant. AccuracySec= can permit a scheduling window, and other system conditions can affect when the service actually starts.

5. Logging and Troubleshooting Scheduled Jobs

A scheduled task is not reliable simply because it appears in a configuration file. Administrators need to confirm that the scheduler triggered it, the command executed, and the operation completed successfully.

Check cron logs

On systems that record cron messages through systemd’s journal, you can inspect them with:

sudo journalctl -u cron

On some distributions, the service is named crond instead:

sudo journalctl -u crond

Other systems may record cron events in files such as /var/log/syslog or /var/log/cron. The exact location depends on the distribution and logging configuration.

If the job does not run, check:

  • Whether the cron daemon is installed and running.
  • Whether the crontab belongs to the intended user.
  • Whether the schedule is valid.
  • Whether the command uses absolute paths.
  • Whether required environment variables are available.
  • Whether file permissions permit execution.
  • Whether standard output and error messages are being captured.

Check systemd timer and service logs

Inspect the timer:

sudo systemctl status daily-backup.timer
systemctl list-timers --all

Read the service’s journal entries:

sudo journalctl -u daily-backup.service

To inspect recent logs:

sudo journalctl -u daily-backup.service -n 100 --no-pager

To follow new log entries:

sudo journalctl -u daily-backup.service -f

Check the service’s exit status and output. A timer can be active even if the associated script repeatedly fails, so checking only the timer status is not enough.

For long-term monitoring, consider alerting on failed service executions, missed backup artifacts, unexpected runtime increases, and low disk space.

6. Managing Dependencies and Execution Environments

Cron starts commands according to its scheduling rules. If a task needs another service, a mounted filesystem, a network connection, or a specific environment, the script may need to check those conditions itself.

Systemd offers a more structured way to express service dependencies and execution requirements.

For example, a service that needs a local filesystem mount could use:

[Unit]
Description=Backup application data
RequiresMountsFor=/srv/application-data

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/application-backup.sh

RequiresMountsFor= adds dependencies on the mount units required to access the specified paths. This helps systemd arrange the required mounts before starting the service.

You can also configure a dedicated execution account:

[Service]
Type=oneshot
User=backup
Group=backup
ExecStart=/usr/local/sbin/application-backup.sh

Before using a dedicated account, ensure that it has the minimum permissions necessary to read the source data and write the backup destination.

Other service settings can define a working directory, environment variables, resource limits, and security restrictions. These settings should be selected based on the script’s actual requirements rather than copied indiscriminately.

Systemd dependencies do not eliminate the need for application-level checks. A network target being active, for example, does not necessarily mean that a remote backup server or external API is reachable.

7. Avoiding Overlapping Job Executions

Both cron and systemd timers need careful design when a task might take longer than its scheduling interval.

Suppose a cleanup job runs every five minutes but sometimes takes eight minutes. A new invocation could be requested while the previous process is still running.

For a cron job, a common solution is to use flock:

*/5 * * * * /usr/bin/flock -n /run/cleanup-job.lock /usr/local/sbin/cleanup-job.sh

The lock prevents another instance from acquiring the same lock while the first execution holds it. Confirm that the selected lock path and permissions are appropriate for the job’s user and runtime environment.

With a systemd timer, repeated timer events targeting the same service do not normally create unlimited simultaneous instances of that same service unit. If the service is already active when another activation is requested, systemd does not start a second instance of that unit.

However, this behavior is not a replacement for application-level locking. A script can launch background processes, invoke other services, or be started manually under another mechanism. Use explicit locking or idempotent processing where concurrent execution would cause data corruption or duplicate work.

8. Which Scheduler Should You Use?

Choose cron when:

  • You need a simple command or script on a predictable timetable.
  • You want a familiar scheduling format that is broadly supported.
  • Your workload does not require systemd-specific service dependencies.
  • Existing scripts and operational tooling already use cron successfully.

Choose systemd timers when:

  • You want scheduled jobs managed as systemd units.
  • You need journal-based logging and service status inspection.
  • You need calendar-based catch-up behavior after a timer was inactive.
  • You want to define dependencies, execution users, working directories, and service restrictions.
  • You need a schedule based on boot time or the activation or completion of a service.

For example, a small script that rotates a custom application report might be perfectly suited to cron. A backup service that depends on mounted storage, runs under a dedicated account, and needs structured logging may be easier to operate as a systemd service and timer.

Neither scheduler is universally better. Choose based on the job’s operational requirements and the tools already supported by your Linux distribution.

9. Production Best Practices for Reliable Scheduled Jobs

Regardless of the scheduler, follow these practices before relying on a job in production.

Test the command independently

Run the script manually using the same account and environment that the scheduler will use, where practical. This helps identify missing permissions, dependencies, and configuration.

Use absolute paths

Avoid depending on the interactive shell’s working directory or environment. Use full paths for executables and scripts.

Make jobs safe to retry

A job may run again after an outage, manual restart, or recovery procedure. Make it idempotent where possible, or record completed work so repeated execution does not duplicate operations.

Set up logging and monitoring

Capture errors and verify the result of important operations. For backups, check the generated artifact, its size and integrity, and periodically test restoration.

Handle failures explicitly

Ensure scripts return meaningful nonzero exit codes when an operation fails. For systemd services, these codes help systemd report failure accurately. For cron, use suitable logging, alerting, or wrapper scripts to surface failures.

Protect credentials

Avoid placing passwords, API tokens, or other secrets directly in crontab entries or broadly readable service files. Use appropriately permissioned configuration files or supported secret-management mechanisms.

Review schedules after system changes

Check scheduled jobs after migrations, user changes, script relocations, and operating system upgrades. Verify time zones and clock synchronization when jobs depend on exact calendar times.

Keep an operational record

Document what each scheduled job does, who owns it, what it depends on, how long it normally runs, and how to recover if it fails. This is especially important on servers hosting business applications.

Conclusion

Systemd timers vs cron is not simply a comparison of two scheduling syntaxes. Cron provides a straightforward and portable way to run commands on a timetable, while systemd timers connect scheduled work to systemd’s service-management features.

Cron remains a sensible choice for simple, established tasks. Systemd timers are often the better fit when you need structured logging, explicit service dependencies, configurable execution environments, or catch-up behavior for missed calendar events.

For reliable Linux server automation, the scheduler is only one part of the solution. Test the underlying script, handle failures, prevent unsafe overlapping executions, monitor results, and design recovery behavior around the consequences of a missed or repeated run.

Official References