Skip to content

Migrating from Crontab to Systemd Timers: A Step-by-Step Guide

Introduction

Systemd timers provide a modern and flexible alternative to traditional cron jobs for scheduling tasks on Linux systems. In this example, we'll consider migrating a daily crontab entry that runs a script at 2:30 AM to a systemd timer.

Why Migrate to Systemd Timers?

  • Increased Flexibility: Systemd timers support more advanced scheduling options than cron, allowing for finer control over when tasks are executed.

  • Dependency Management: Systemd timers can express dependencies between different units, ensuring tasks are executed in a specified order.

  • Improved Logging: Systemd captures detailed logs for each timer and associated service, aiding in troubleshooting and monitoring.

Steps for Migration

1. Identify the Crontab Entry to Migrate

Consider the following crontab entry:

30 2 * * * /path/to/your/script.sh

This crontab entry schedules the execution of /path/to/your/script.sh every day at 2:30 AM.

2. Create a Systemd Service

Create a systemd service unit file (e.g., mytask.service). Place it in the /etc/systemd/system/ directory.

[Unit]
Description=My Task Description

[Service]
Type=simple
ExecStart=/path/to/your/script.sh

3. Test the Service

Ensure that the service can be started and runs as expected. Test it by executing:

sudo systemctl start mytask.service

4. Create a Systemd Timer

Create a systemd timer unit file (e.g., mytask.timer). Place it in the /etc/systemd/system/ directory.

[Unit]
Description=My Task Timer

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

5. Enable and Start the Timer

Enable the timer to ensure it starts on boot:

sudo systemctl enable mytask.timer

Start the timer:

sudo systemctl start mytask.timer

6. Monitor and Troubleshoot

Check the status and logs to ensure the timer and service are running correctly:

sudo systemctl status mytask.timer
sudo journalctl -u mytask.service

7. Remove Crontab Entry

Once you have verified that the systemd timer is working correctly, you can remove the corresponding crontab entry.