Using systemd as a better cron

Mar 7, 2018 · 4 min read

systemd is a pretty great way to schedule cron like tasks using Linux. In the past, I’ve used everything from cron to full blown distributed task scheduling systems. For my latest project (, I had been using a pretty hacky combo of tmux, while, and sleep on a tiny Digital Ocean server, which eventually became cumbersome. I needed to find a better alternative.

My requirements for a task scheduler were:

  • Run some quick tasks every 15 minutes
  • Run some long running tasks every 12 hours (sometimes these tasks take over 30 hours)
  • Don’t run a task if the same type of task is already running. For example, don’t start a new instance of the scraper if an old instance is still running.

After learning about systemd, I learned that it met all my requirements and had some pretty awesome benefits:

  • Automatic log rotation
  • Random delays: add RandomizedDelaySec to a timer
  • Memory and CPU limits: add CPUQuota or MemoryLimit to a slice
  • Easy to debug: systemctl lets you view logs, exit codes, and timer history

I’m still a novice at using systemd, so please keep in mind that the configuration and commands below may be incorrect.

My systemd setup scrapes around 60 sneaker stores. My first attempt at using systemd involved setting up a recent import service and a full import service for each of the stores. Unfortunately, whenever I installed and enabled these 120 services, the server would run out of memory and CPU and need to be power cycled. I spent a few days trying out different configurations, but at the time I couldn’t avoid having too many node processes starting at once. So, instead I grouped the sites by platform and now run only 10 services.

To setup my cron-like system, I used these systemd features:

  • Services describe the work do. I have five recent import services and five full import services. The only real difference between each service is the command run in ExecStart.
  • Timers describe when to run the work. I have five recent import timers and five full import timers. The recent scrapers run every 15 minutes, while the full imports run every 12 to 24 hours.
  • Slices describe resource limits. I have one slice that limits the resources used by all my services as a whole.


Notice that ExecStart uses the full path to node. WorkingDirectory launches the script in the same folder as my code.


This timer runs every 15 minutes + some random duration below 15 minutes.


Because all my scrapers depend on shoes-scraper.slice , I can make sure my scrapers collectively never use more than 80% of the CPU or more than 2.7G of RAM on my server. Doing something like this with cron would be pretty tough.

Installing the services

To install and start these services I run:

If something went wrong when setting up a service, you can usually find it out by running systemctl status .

I don’t like running these commands every time I make a change, so I wrote a script to generate and install my systemd configuration.

Handy commands


In conclusion systemd can be a nicer alternative to cron. Compared to cron, it can take a lot of time to configure systemd. But, systemd makes other things easier, like debugging errors, setting CPU and memory limits, and randomized scheduling.

I shared this blog on Reddit and got some pretty great feedback. Here’s some I’d like to highlight:

  • Myth: Email alerts are easier with cron. Actually, you can use OnFailure= with a systemd service to call the mail command.
  • You can use flock with cron to prevent running multiple instances of the same task.

Horrible Hacks

Some javascript and horrible stuff

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store