The Linux terminal is a powerful way to run programs on your computer from the command line. Using scripts, you can easily repeat common tasks, even using complex programming logic.
But working with automations can be awkward, especially if you’re not a fan of the terminal. Fortunately, Linux provides a couple of convenient solutions for task management, whether you need to schedule repeating programs or run them on demand.
What to automate and why
If you need to do it more than once, automate it
First, what might you want to automate in the first place, and why?
One of the things I’m terrible at is clearing out my Downloads folder; over time, it grows and grows, and then I end up just deleting everything manually:
rm -rf ~/Downloads/*
While this works OK, it’s a pain to have to keep doing it, so I like to automate the process. Automating a task has two main benefits: it saves you time, and it gives you greater control over the task. Running a script from the terminal is as simple as typing a file name or command and pressing Enter:
# run a command from your PATH
clear-downloads
# run a command from a file
~/.local/bin/clear-downloads
This is, of course, much easier and quicker than running the specific commands that those scripts or programs may contain. Using a script also lets me add related functionality: I can record details of what’s been deleted in a log or filter the files based on size or age, for example.
Anything that you might want to do more than once is a candidate for automation. Keeping a record of network traffic, reporting disk usage, updating software, or updating an RSS feed are all good examples.
How to automate with cron
The classic way to schedule tasks on Linux
For half a century, cron has been the main way to schedule tasks to run at certain times. Cron sticks to the classic Unix principle that plain text is superior, and simple formats are the best:
A crontab file contains a list of tasks to run, each of which is in the format:
m h d o w
In this format, m (minute), h (hour), d (day of month), o (month), and w (day of week) are numbers defining when to run a task, so you can set a task to run at 10:00 on every Friday with a line like this:
* 10 * * 5 /Users/bobby/.local/bin/clear-downloads
While cron has endured, it has its critics. The syntax isn’t always easy to remember—or even understand—the environment that cron runs in isn’t obvious, it can be difficult to troubleshoot, and tasks are strictly tied to a clock time. Cron can be the right tool for simple jobs, but there’s a much more powerful, improved alternative: systemd.
How to automate with systemd and why it’s better
Providing everything that cron does, and so much more
The systemd software is a more modern approach to service management, which does what cron does, plus a lot more. You can still use it to schedule scripts, but systemd also handles system processes, boot management, event logging, and network resolution.
A systemd task takes a bit more initial setup than a cron entry, but it’s much more usable once you’ve created it. As a simple example, imagine you have a script at /home/bobby/scripts/clear-docs.sh:
#!/bin/sh
# Remove everything. Can be modified to remove only a certain number of files,
# only files above a certain size, files older than a certain age, etc.
rm –recursive –force –verbose ~/Documents/*
To install this as a scheduled systemd task (called a timer), create an accompanying file at /etc/systemd/system/clear-docs.service:
[Unit]
Description=Cleans out the Documents directory as a demo of systemd timers
Wants=clear-docs.timer
[Service]
User=bobby
Group=bobby
Type=oneshot
ExecStart=/home/bobby/scripts/clear-docs.sh
[Install]
WantedBy=multi-user.target
Note that the format of this file is very much like the Windows .ini file format, with headed sections in brackets and name/value pairs on each line.
The Wants= directive is a good example of how systemd goes a lot further than cron. It specifies a dependency on another unit, which you can create in a separate file at /etc/systemd/system/clear-docs.timer:
[Unit]
Description=Timer for: clear-docs.service
Requires=clear-docs.service
[Timer]
Unit=clear-docs.service
# Run the timer, every hour, on the hour
OnCalendar=*-*-* *:00:00
[Install]
WantedBy=timers.target
This timer definition uses the OnCalendar directive, which looks a bit like the prefix that cron uses to schedule each command. However, the systemd software provides much greater flexibility: you can set automations to respond to relative times like “ten minutes after boot” (OnBootSec=10min) or even events unrelated to time, like when a specific file is modified.
With those files created, you can now complete the setup by restarting systemd and starting your new service:
sudo systemctl daemon-reload
sudo systemctl enable clear-docs.timer
sudo systemctl start clear-docs.timer
At this point, you can check that everything is up and running by querying the timer:
systemctl status clear-docs.timer
Note that the information returned includes when the timer started and when it will next trigger.
You can also query the service, which gives details of the script file and brief logs:
Controlling systemd with a GUI
Use one of these apps for easy access to automations
With the hard work out of the way, you can control individual systemd services using a front-end app like Systemd Manager.
This GTK-based app lets you view services, enable individual units, and edit automation files. You can also check their logs and analyze the time it takes to boot your system.
For day-to-day use, I prefer the GNOME extension of the same name, which is a much simpler alternative. It adds a drop-down to your top panel, with individual services that you can choose to include.
Using this extension, you can start, stop, and restart a service with just two clicks. It’s always available, so you don’t need to go anywhere near your terminal to run automations, even those that are normally scheduled for specific times. Running a job on demand can help you debug it, and systemd makes this a lot easier than cron does.
For unparalleled flexibility, systemd is the way
Not everyone is a fan of systemd, and I can see why. For those who grew up with cron, it presents a steeper learning curve, and its complexity doesn’t always abide by Linux principles.
However, there’s no denying the benefit of scheduled tasks that run in a controlled environment, with advanced features like logging built in. With supporting GUI apps offering one-click controls and a terminal-free experience, systemd is well worth your time.

