Technical skill

Automation

Subject: .../LINUX

Make a repeated task run by itself

Suppose you want a computer to report how much disk space is free every night. Doing it by hand works once, but a scheduled job can run the same steps at a chosen time. Automation means the computer repeats a task according to instructions. A job needs a command to run, a trigger that says when to run it, the account that runs it, and a result you can check.

Start with a small script that prints space use for the root filesystem:

#!/usr/bin/env bash
set -eu
/usr/bin/df -P /

The first line tells the system to use Bash. set -eu makes the script stop on a failed command or an unset variable. df reports space on a filesystem, the way the operating system organizes files on a disk. -P asks for a predictable, one-line-per-filesystem layout, and / names the top of the directory tree. df reports the filesystem mounted there, often called the root filesystem. Run the script yourself and confirm the output before scheduling it. This example only reads information, so repeating it does not create a second change.

Give the job its own environment

A scheduled task may not start with the same settings as your terminal. It may have a smaller PATH (the list of folders searched for commands), no aliases (short names defined by your shell), and a different working directory. Put repeated commands in a script, give it a shebang that names the interpreter, use explicit command paths where needed, and set the working directory and required environment deliberately. Send useful output to a log or system journal, the event log used by systemd. Return a nonzero exit status (a result number that signals failure) if the task fails so the scheduler or a person can detect the problem.

Cron is a scheduler that runs commands from time-based entries. A user's crontab runs jobs as that user. System crontab formats and directories differ among implementations, and the cron service must be installed and running. Cron works well for straightforward calendar schedules, but check the local manual for its exact syntax and environment.

On systems that use systemd, a timer configuration activates a matching service configuration. Systemd is the service manager, the system software that starts and manages background programs. The service unit says which program to run and its settings; the timer unit says when to run it. OnCalendar sets calendar times. Persistent=true can cause a missed calendar event to run after a timer becomes active again, which can help a maintenance task catch up after downtime. Use the scheduler available on the system, and do not create two schedules for the same task.

Check a scheduled run

A systemd service for the script can use Type=oneshot and ExecStart=/usr/local/sbin/check-root-space. A companion timer can use OnCalendar=*-*-* 02:15:00 and Persistent=true. The timer and service should share a base name, such as check-root-space.timer and check-root-space.service. After installing the unit files, reload systemd's unit definitions, then enable and start the timer according to local policy. systemctl list-timers shows timer state. Run the service manually once, then inspect its result and logs after a scheduled run.

Give each job an owner, purpose, and recovery procedure. Try a missing input, a permission error, and an external command that fails. Keep credentials out of scripts and command-line logs. Grant only the permissions the task needs; give the script access to its log instead of running the whole program as root. Keep output with deliberate access permissions and retention. Cron details, systemd availability, commands, and logging locations vary by operating system and version, so check the local configuration.

Interactive exercises

Materials

Skillsets
Skillset Linux administrator preview
image descriptionLinux administrator
by Infinite Education
3080 users
Skillset Devops preview
image descriptionDevops
by Ajay Kumar
2004 users
Skillset DevOps engineer preview
image descriptionDevOps engineer
by Infinite Education
1211 users
Skillset DevOps preview
image descriptionDevOps
by Federico Cordone
3 users
Skillset Devops preview
image descriptionDevops
by Dio
3 users
Do you know that ...?
Any useful or interesting facts about the skill. Any peace of information would be useful