Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
DevOpsJune 21, 20264 min read

Systemd Timers: The Better Way to Handle Linux Automation

Systemd timers provide a robust, observable alternative to cron for Linux automation. Learn how to manage server maintenance tasks with native systemd features.

linuxsystemddevopssysadminautomationscriptingDockerCI/CD
Close-up of a smart socket with vibrant blue and pink neon lighting, showcasing modern technology.

I spent my first few years as a sysadmin relying on crontab for everything. If a script needed to run at 3:00 AM, it went into /etc/crontab. It was simple until it wasn't—logs disappeared into the void, tasks overlapped because of execution delays, and debugging required parsing cryptic shell syntax.

Transitioning to systemd timers completely changed how I handle background tasks. By treating maintenance scripts as first-class systemd units, you gain native logging, dependency management, and clear status reporting that cron simply can't touch.

Why move away from Cron?

Cron is a legacy tool. It’s excellent for "fire and forget" tasks, but it lacks the operational rigor required for modern production servers. When a cron job fails, you rarely get a notification unless you’ve explicitly piped output to a mail agent—which most of us don't configure anymore.

When I started running background workers with systemd for production reliability, I realized that the same unit-based approach applies perfectly to maintenance scripts. With systemd, you can define OnFailure= triggers, set resource limits with MemoryMax, and inspect the exact exit status of the last run using systemctl status.

Anatomy of a Systemd Timer

A red timer sits on a dusty windowsill with a chain link fence background, evoking nostalgia.

To replace a cron job, you need two files: a .service file to define what to do, and a .timer file to define when to do it.

Let's say we want to rotate application logs and clear a temp directory every day at 4:00 AM.

1. The Service File: /etc/systemd/system/cleanup.service

This is the workhorse. Note that I don't include the timer logic here; this service is purely functional.

INI
[Unit]
Description=Cleanup temporary application files

[Service]
Type=oneshot
ExecStart=/usr/local/bin/cleanup-script.sh
User=root

2. The Timer File: /etc/systemd/system/cleanup.timer

This is where the linux automation happens. The syntax is more verbose than cron, but it’s significantly more readable and powerful.

INI
[Unit]
Description=Run cleanup daily

[Timer]
OnCalendar=*-*-* 04:00:00
Persistent=true

[Install]
WantedBy=timers.target

The Persistent=true flag is the killer feature. If the server is powered off at 4:00 AM, systemd will trigger the task immediately upon boot. Cron would simply skip the execution until the next day.

Managing Server Maintenance Tasks

Once you've moved your server maintenance tasks to timers, you interact with them using standard systemd commands. You don't need a separate tool to see what's queued or when a task last ran.

Run systemctl list-timers to see every active timer on your system. It gives you a clean view of the next execution time and the last time the service was triggered.

Bash
NEXT                         LAST                         UNIT                         ACTIVATES
Wed 2023-10-25 04:00:00 UTC  Tue 2023-10-24 04:00:00 UTC  cleanup.timer                cleanup.service

If you need to debug a failure, journalctl -u cleanup.service shows you exactly what happened during the last run. You aren't hunting through /var/log/syslog or checking email headers. Everything is centralized in the journal, which is about as good as it gets for local debugging.

Advanced Scheduling and Constraints

What if you have a background tasks script that is resource-intensive? Cron doesn't care about system load. Systemd does.

You can add Nice=19 or IOSchedulingClass=idle to your .service file to ensure your maintenance tasks don't starve your primary application processes. I’ve found this essential when running database backups on smaller cloud instances.

Also, consider using AccuracySec in your timer file. If you have a task that doesn't need to run at the exact second, setting an accuracy window allows systemd to group your task with other system events, reducing the frequency of wake-ups and saving a bit of CPU context switching.

Lessons Learned

I've made my share of mistakes while migrating. Here’s what I’d do differently if I were starting over:

  1. Don't over-engineer: Start by porting existing cron jobs one by one. Don't try to build a complex dependency chain between timers unless you absolutely have to.
  2. Use OnUnitActiveSec for intervals: If you need a script to run every 30 minutes, use OnUnitActiveSec=30min instead of OnCalendar. It’s cleaner for relative timing.
  3. Watch the logs: If a service fails, systemd will tell you via systemctl status. If you're managing a fleet, consider shipping these logs to a central collector.

I still use cron for simple, user-level tasks that don't need monitoring. But for anything involving production reliability, systemd timers are my default choice. They provide the observability I need to sleep better at night.

Are there times when cron is actually better? Maybe for very simple, ephemeral scripts on a dev machine where you don't want to clutter /etc/systemd/system/. But for production infrastructure, the trade-off of writing two files instead of one line is worth the peace of mind.

Back to Blog

Similar Posts

Deliveryman in red uniform delivers package outdoors in daylight.
DevOpsJune 20, 20264 min read

Running background workers with systemd for production reliability

Running background workers with systemd is the gold standard for process management. Learn to write robust service files to keep your tasks alive.

Read more
Close-up of software development tools displaying code and version control systems on a computer monitor.
DevOps
June 20, 2026
4 min read

Deploying a side project on a single cheap VPS reliably

Deploying a side project on a single cheap VPS is the best way to master infrastructure. Learn to secure, automate, and manage your stack without breaking.

Read more
A vibrant display of love locks attached to a fence with colorful ribbons.
DevOpsJune 21, 20264 min read

WireGuard Mesh Networking: Secure Your VPS Cluster Communication

WireGuard mesh networking is the most efficient way to secure inter-node communication. Learn to build a private, encrypted VPS cluster network today.

Read more