Linux on call · Chapter 22 of 30
Scheduled jobs with cron
By the end of this chapter you can make the machine run a command on a schedule, read and fix a crontab line, and give a job the full paths and the log it needs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Processes and the system
- 4 tasks
- Linux
- In the all labs plan
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.
Why you need this
Arjun set up a nightly clean-up of the menu-image cache before he left. Ravi read last night's logs: it ran sixty times, once a minute from 2:00 to 2:59. Kabir also wants the order report every five minutes, with everything it prints kept in a log. Their notes are in note.txt.
So far you run a script when you type it. Nobody is awake at 2:00, and a sleep loop in the background cannot say "every Monday", and dies with its terminal. Arjun's line is in his crontab, which you now own:
crontab -l
# Arjun: nightly cache clean-up for /workspace/chapters/22-cron, meant for 2 am
* 2 * * * /workspace/chapters/22-cron/bin/clean-cache.shWhat you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Run Arjun's cache clean-up once a night, at 2:00
Fix Arjun's line in your crontab. Now it runs sixty times a night, but it should run once.
1. Run crontab -l. Read the five time fields of Arjun's line.
2. Change only the minute field, so the job runs at 2:00 exactly. Keep the hour and the three stars.
3. Install the changed crontab, with crontab -e or with a pipe into crontab -.
4. Run crontab -l again to see the new line.
To pass: your crontab has exactly one line that runs clean-cache.sh, and it runs at 2:00 every night, on every day of every month.
- Task 2
Run the order report every 5 minutes, by its full path
Add a line to your crontab that runs the order report every 5 minutes.
1. Write the minute field so it means every 5 minutes. The other four fields are stars.
2. After the five fields, write the script's full path: /workspace/chapters/22-cron/bin/order-report.sh. cron starts in your home folder, so a short path does not work.
3. Add the line to the end of your crontab. Keep the lines that are already there.
4. Run crontab -l to see all the lines.
To pass: your crontab has exactly one line that runs order-report.sh, every 5 minutes, every hour, every day, with its full path. Arjun's clean-up line stays.
- Task 3
Add the report's output and errors to logs/report.log
A cron job has no screen. Send everything the report prints into a log file.
1. On the order report line, after the script, add >> and the log's full path: /workspace/chapters/22-cron/logs/report.log. >> adds to the end, so older lines stay.
2. Right after the log's path, add 2>&1. It sends the errors to the same file.
3. Change the report line you already have. Do not add a second report line.
To pass: your crontab has exactly one order report line, and it has >> with the log's full path, and 2>&1 right after it, in that order.
- Task 4 · interview question
Interview: the schedule for every Monday at 9:30
Interview question: how do you write "every Monday at 9:30 in the morning" for cron?
1. Write the five time fields in order: minute, hour, day of month, month, day of week.
2. Save them in schedule.txt, in the chapter folder. A command after them is allowed, but not needed.
3. Run cat schedule.txt to see what you saved.
To pass: the first five fields in schedule.txt are the cron schedule for every Monday at 9:30 in the morning.
What you will learn
- cron and the crontab: the service that runs jobs on a schedule, and your own table of jobs.
- The five time fields: minute, hour, day of month, month, day of week, and what and /N mean.
- Changing the crontab from a pipe: adding and removing lines, and the one command that wipes them all.
- Cron's world: no terminal, no current folder, a short PATH.
More in Processes and the system
Build "Scheduled jobs with cron" yourself
Tapri’s ordering app moves onto a Linux server, and the server is now yours to run. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Linux on call are free. This one comes with the all labs plan.