Systemd Timers Cron Alternative

Systemd Timers Cron Alternative

- 5 mins

A Beginner’s Guide to Systemd Timers

Introduction

Systemd timers offer a modern and flexible way to schedule automated tasks in Linux. Unlike traditional cron jobs, systemd timers integrate directly with systemd services, providing enhanced logging, better dependency management, and improved error handling.

We can use systemd timers insteaed of cron

In this guide, you’ll learn how to create and manage systemd timers with practical examples.


What Are Systemd Timers?

A systemd timer is a unit file in systemd that schedules tasks at specific intervals or times. It serves as a replacement for cron while offering:

To list all available timers on your system, run:

systemctl list-timers --all

or

systemctl status *timer

timer

timer


How Systemd Timers Work

Systemd timers consist of two main unit files:

  1. Timer Unit File – Defines when a task should run.
  2. Service Unit File – Specifies what task should be executed.

For example, to run a Memory check script every minutes:

You can control timers using standard systemctl commands:

systemctl start mytimer.service  
systemctl stop mytimer.service
systemctl enable mytimer.timer  
systemctl status mytimer.timer  

Understanding Systemd Timer Expressions

Systemd timers use a structured time format:

* *-*-* *:*:*
│ │ │ │ │ │ │
│ │ │ │ │ │ └──── Seconds (0 - 59)
│ │ │ │ │ └────── Minutes (0 - 59)
│ │ │ │ └──────── Hours (0 - 24)
│ │ │ └────────── Day of Month (1 - 31)
│ │ └──────────── Month (1 - 12)
│ └────────────── Year
└──────────────── Day of Week (Sun - Sat)

Example Expressions

Schedule Systemd Timer Expression Description
Every 10 minutes *-*-* *:0/10:00 Runs every 10 minutes
Every Monday at 8 AM Mon *-*-* 08:00:00 Runs every Monday at 8 AM
First of every month *-*-01 00:00:00 Runs at midnight on the 1st of each month
Every weekday at noon Mon..Fri *-*-* 12:00:00 Runs at noon from Monday to Friday

Creating a Systemd Timer

  1. First, create a service file to define the task.
  2. I will use free command as an example and I will check free memory on the box
  3. For systemd service creation You need to create unit file
vi /etc/systemd/system/myMemoryMonitor.service 

timer

  1. Let’s Check status of service
    systemctl status myMemoryMonitor.service 
    
  2. Let’s start the sevice
    systemctl start myMemoryMonitor.service 
    

timer

  1. As You can see above output myMmyMemoryMonitor Service started and finished after free command ran.
  2. We can check journal logs to see more detailed service logs.
journalctl -S today -u myMemoryMonitor.service 

timer

vi /etc/systemd/system/myMemoryMonitor.timer 

timer

  1. We can enable timer service
    systemctl start myMemoryMonitor.timer 
    
  2. We can wtach logs real time to see what services will do.
    journalctl -S today -f -u myMemoryMonitor.service
    
  3. If you start timer myMemoryMonitor.service will run every minutes.

timer

Step 3: Enable and Start the Timer

sudo systemctl daemon-reload
sudo systemctl enable myMemoryMonitor.service
sudo systemctl start myMemoryMonitor.service
systemctl list-timers --all

timer

Conclusion

Systemd timers provide a robust, flexible alternative to cron jobs with better logging and service management. By learning to use systemd timers, you can automate tasks efficiently while maintaining system stability.


You can also check out this article: https://coady.tech/systemd-timer-vs-cron/)



Thanks for reading…





:+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1:


Guneycan Sanli.


Guneycan Sanli

Guneycan Sanli

A person who like learning, music, travelling and sports.

comments powered by Disqus