Yahoo
Advertisement
Advertisement
Advertisement
Advertisement

How I use the Linux terminal without touching it: My secret to extreme automation

systemd logo and the Linux mascot using a laptop in front.
Lucas Gouveia/How-To Geek

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?

Advertisement
Advertisement

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:

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:

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 with many comments explaining the format and several entries, each with a series of numbers to define its frequency and a command that will run at the given time.

A crontab file contains a list of tasks to run, each of which is in the format:

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:

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.

Advertisement
Advertisement

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:

To install this as a scheduled systemd task (called a timer), create an accompanying file at /etc/systemd/system/clear-docs.service:

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:

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.

Advertisement
Advertisement

With those files created, you can now complete the setup by restarting systemd and starting your new service:

At this point, you can check that everything is up and running by querying the timer:

Note that the information returned includes when the timer started and when it will next trigger.

Status information for a systemd timer showing its start time and that it will trigger in just under five minutes.

You can also query the service, which gives details of the script file and brief logs:

The status of a systemd service showing a path to the script it will run and logging details.

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 .

The Systemd Manager app showing a list of services in one column and details of a Unit File in the main panel.

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.

Advertisement
Advertisement

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.

linux-auto-systemd-ext-1

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.

Advertisement
Advertisement
Mobilize your Website
View Site in Mobile | Classic
Share by: