On this page

This chapter covers two kinds of automation: scheduled tasks with cron, and small shell scripts that bundle commands.

cron — the scheduler

cron runs commands on a schedule. It is a service that is active by default on most distros. Each user has their own crontab — a file listing what to run and when.

crontab — manage scheduled tasks

Edit your crontab:

$ crontab -e

This opens your text editor with an empty crontab. List it:

$ crontab -l

Each line has five time fields followed by the command:

minute hour day-of-month month day-of-week  command
 0      2    *            *         *        /usr/bin/backup.sh

The line above runs /usr/bin/backup.sh every day at 02:00.

Field Range
minute 0–59
hour 0–23
day of month 1–31
month 1–12
day of week 0–7 (0 and 7 are Sunday)

* means “every”. Examples:

# every day at 02:00
0 2 * * * /usr/bin/backup.sh

# every hour, at minute 30
30 * * * * /home/user/check.sh

# every Monday at 09:00
0 9 * * 1 /home/user/weekly.sh

# every 15 minutes
*/15 * * * * /home/user/status.sh

After saving, cron picks up the new schedule automatically. Check the cron service is running if nothing fires:

$ systemctl status cron

To install a crontab from a file (handy for deploying): write the lines to a file and load it:

$ crontab my_crontab.txt

Remove your crontab entirely:

$ crontab -r

A warning about paths: cron runs commands with a minimal environment, so backup.sh will not find your ~/.bashrc aliases or $PATH entries. Use absolute paths (/usr/bin/... and /home/user/...) in cron lines and scripts.

Simple shell scripts

A shell script is a file containing commands, run top to bottom. Create one with the editor from Chapter 4:

$ nano hello.sh

Type these two lines, then save and exit (Ctrl+O, Enter, Ctrl+X):

#!/bin/bash
echo "Hello from script"

The first line (#!/bin/bash) is the shebang — it tells the system which shell to use. Make the script executable and run it:

$ chmod +x hello.sh
$ ./hello.sh
Hello from script

You can also run it without making it executable:

$ bash hello.sh

Scripts can use everything from this guide — pipes, redirection, variables, $1 for the first argument:

#!/bin/bash
# backup.sh — copy a directory to ~/backups with a date stamp
dir="$1"
stamp="$(date +%Y%m%d)"
cp -r "$dir" "$HOME/backups/$dir-$stamp"
echo "Backed up $dir to backups/$dir-$stamp"

The $( ) around date runs the command and uses its output right there — so stamp ends up holding the formatted date.

Run it with an argument:

$ chmod +x backup.sh
$ ./backup.sh Documents
Backed up Documents to backups/Documents-20260805

The date stamp comes from the date command. On its own it prints the current date and time; with +FORMAT it prints whatever the format says. %Y%m%d expands to year-month-day (for example 20260805), which is a convenient, sortable name for a backup folder.

$ date
Wed Aug  5 10:00:00 UTC 2026
$ date +%Y%m%d
20260805

Combine the two ideas: put the script in cron and you have unattended automation — the classic “do a backup every night at 02:00” setup.

When a script outgrows its first five lines, the guide Bash — Scripts That Hold Up goes deeper into quoting, tests, loops and failure handling.