The nightly report nobody ran
A shop wants its stock report written at six every Monday morning. For months, one tired person ran it by hand.
One Monday they were ill, and no report came.
A machine should do work that repeats. It should also keep its important programs running without help.
By the end of this lesson
Note
I can run a program as a service and schedule a job.
Key words
| Word | What it means | Example |
|---|---|---|
| Service | a program the system keeps running in the background | |
| systemd | the program that starts and watches services | systemctl |
| Unit file | a small file that tells systemd about one service | |
| Enable | make a service start every time the machine boots | |
| Cron | the program that runs jobs at set times | |
| Cron job | one line that gives a time and a command | 15 6 * * 1 |
The cells students fill in are left blank in this preview.
systemd runs services
A service is a program that should always be running, such as a web server or a mailer. On Ubuntu, systemd starts them at boot and watches them.
systemctl is how you talk to systemd. You can ask whether a service is running (is-active) and whether it starts at boot (is-enabled).
Is the cron service running?
systemctl is-active cronShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Does it start at boot?
systemctl is-enabled cronShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The whole story
systemctl status cron --no-pagerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The unit file systemd reads for cron
systemctl cat cron --no-pagerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Worked example: the verbs of systemctl
| Command | What it does | Survives a reboot? |
|---|---|---|
| systemctl start mailer | Starts it now | No |
| systemctl stop mailer | Stops it now | No |
| systemctl restart mailer | Stops it, then starts it | No |
| systemctl enable mailer | Makes it start at boot | Yes |
| systemctl disable mailer | Stops it starting at boot | Yes |
The cells students fill in are left blank in this preview.
A unit file has three parts
A unit file is plain text. One you write goes in /etc/systemd/system. One that comes with a package sits in /usr/lib/systemd/system. [Unit] describes the service. [Service] says what to run in ExecStart and whether to bring it back after a failure in Restart. [Install] says when it should start. The next block shows one for a program called shop-mailer.
A unit file for another program
[Unit]
Description=Shop mailer
[Service]
ExecStart=/usr/local/bin/shop-mailer
Restart=on-failure
[Install]
WantedBy=multi-user.target
A common mistake
Warning
Many people think systemctl start also makes a service come back after a reboot. It does not. start runs it now. enable makes it start at boot. A service that needs both gets both.
Cron runs a command at set times
A cron line has five time fields, then the command. The fields are minute, hour, day of the month, month and day of the week.
A star * means every one. A number means exactly that one. In the minute field, */10 means every ten minutes: 0, 10, 20 and so on.
Worked example: reading cron times
| Fields | It runs |
|---|---|
| 15 6 * * 1 | At 06:15 every Monday |
| 0 * * * * | At the start of every hour |
| */10 * * * * | Every ten minutes |
| 0 9 1 * * | At 09:00 on the first of each month |
| 30 23 * * 5 | At 23:30 every Friday |
The cells students fill in are left blank in this preview.
Your own list of jobs
crontab -lShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Two example lines for cron
# minute hour day month weekday user command
15 6 * * 1 root /usr/local/bin/weekly-report.sh
*/10 * * * * root /usr/local/bin/sync-stock.sh
The system folder has one extra field
A user's own list, shown by crontab -l, has the five times and then the command.
A file in /etc/cron.d also has the user to run as, between the times and the command. Cron skips a file it does not like and says nothing on screen. The owner must be root, and nobody else may write to it. The name must have no dot, so report.cron is skipped. The last line must end with a new line.
The system's folder of jobs
ls -l /etc/cron.dShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
A script waiting to be scheduled
cat /usr/local/bin/backup.shShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
systemd has its own timers too
systemctl list-timers --no-pagerShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Going further
Cron reads 1-5 in the weekday field as Monday to Friday. On paper, write the line for a file in /etc/cron.d that runs /usr/local/bin/sync-stock.sh as root at 22:45 on weekdays only.
Then say when it runs next if it is now Friday at 23:00.