Scheduling Scripts with Cron Jobs
Run Linux commands and shell scripts automatically at fixed times with cron, read the five fields of a cron expression, and manage jobs with crontab.
The scripts from the last lesson take a backup, print a health report and check a service. They all share one weakness: somebody has to remember to run them. A backup that depends on a person typing ./backup.sh every night at ten will be missed sooner or later.
Linux has a built-in answer called cron. Cron is a time-based job scheduler. You tell it once which command to run and when, and from then on it runs the command by itself, for as long as the machine is up.
Think of the alarm on your phone. You set it once for 6 AM on weekdays and it rings on its own. A cron job is an alarm for your server, and in place of a ringtone it runs a command or a script.
Typical uses:
- Take a backup every night
- Check the health of the system every few minutes
- Delete old log files once a week
- Send a report every Monday morning
How cron works
Three names come up all the time:
| Name | What it is |
|---|---|
| cron | The service. It runs in the background all the time as a daemon, wakes up every minute and checks whether a job is due |
| crontab | Short for cron table. The file that lists your jobs, one per line. It is also the name of the command you use to edit that file |
| cron job | One line in the crontab: a schedule followed by a command |
On Ubuntu the cron service is installed and running by default. You can confirm it with systemctl status cron.
The cron expression
Every job starts with five fields that describe when it should run, followed by what to run:
* * * * * <command or script>The position of each field is fixed, and this order is the one thing you really have to memorise:
| Position | Field | Allowed values |
|---|---|---|
| 1 | Minute | 0 to 59 |
| 2 | Hour | 0 to 23 |
| 3 | Day of the month | 1 to 31 |
| 4 | Month | 1 to 12 |
| 5 | Day of the week | 0 to 6, where 0 is Sunday, 1 is Monday and 6 is Saturday |
A * in a field means "every". So five stars mean every minute of every hour of every day of every month, on every day of the week. In short: once a minute, all year.
Two things about time:
- There is no AM or PM. Hours use the 24 hour clock, so 10 PM is
22and 6 PM is18. - The whiteboard in the lecture labels the last field "Sun-Mon". The full range is Sunday (
0) to Saturday (6).
Reading examples
| Expression | When it runs |
|---|---|
* * * * * /home/ubuntu/test.sh | Every minute |
0 22 * * * /home/ubuntu/backup.sh | Every day at 10 PM |
0 10 * * 1 <script> | Every Monday at 10 AM |
0 18 * * 1-5 <script> | Monday to Friday at 6 PM |
Read the second one field by field: minute 0, hour 22, any day of the month, any month, any day of the week. That is the first minute of 10 PM, every day.
The 0 in the minute field is important. * 22 * * * would not mean "at 10 PM". It would mean every minute from 10:00 PM to 10:59 PM, sixty runs in a row.
More than a single value
The last example uses 1-5, a range. There are three more symbols like it:
| Symbol | Meaning | Example | When it runs |
|---|---|---|---|
* | Every value | * * * * * | Every minute |
- | A range | 0 18 * * 1-5 | 6 PM, Monday to Friday |
, | A list | 0 9,18 * * * | 9 AM and 6 PM, every day |
/ | A step | */5 * * * * | Every 5 minutes |
A few schedules you will write again and again:
| Expression | When it runs |
|---|---|
*/15 * * * * | Every 15 minutes |
0 * * * * | Every hour, on the hour |
0 0 * * * | Every day at midnight |
30 2 * * 0 | Every Sunday at 2:30 AM |
0 0 1 * * | At midnight on the first day of every month |
Step 1: A script to schedule
To see cron at work we need a script that leaves a trace each time it runs. This one adds a line with the current date and time to a log file. Create it in the home directory:
cd ~
vi cron-demo.sh#!/bin/bash
echo "Cron job executed at $(date)" >> /home/ubuntu/cron.log
echo "Log entry added at $(date)"The first line uses >>, which appends to the file. A single > would overwrite the file on every run and you would only ever see the last entry. The second line prints a message on the screen.
There is no loop in this script. It runs once and stops. If it runs again and again, something outside the script is starting it.
Test it by hand first:
chmod u+x cron-demo.sh
./cron-demo.shLog entry added at Tue May 12 06:49:11 UTC 2026cat cron.logCron job executed at Tue May 12 06:49:11 UTC 2026One run, one line.
Always test by hand first
If a script does not work when you run it yourself, it will not work under cron either. And under cron you will not see the error. Make it work manually, then schedule it.
Step 2: Schedule it with crontab -e
Open your cron table for editing:
crontab -eThe first time, Ubuntu may ask which editor to use. Choose nano, the simplest one. The file opens with some comment lines that explain the format. The last comment line is a small reminder of the field order:
# m h dom mon dow commandGo to the end of the file and add your job on a new line:
* * * * * /home/ubuntu/cron-demo.shFive stars, so every minute, and then the script with its full path.
Save and close. In nano that is Ctrl + O, then Enter to confirm the file name, then Ctrl + X to exit. Back in the terminal you see:
no crontab for ubuntu - using an empty one
crontab: installing new crontabThe second line is the confirmation. The job is now scheduled. You do not restart anything, cron notices the change by itself.
Step 3: Watch it run
Wait a couple of minutes without touching anything, then look at the log:
cat cron.logCron job executed at Tue May 12 06:49:11 UTC 2026
Cron job executed at Tue May 12 06:52:01 UTC 2026
Cron job executed at Tue May 12 06:53:01 UTC 2026The first line is from the manual run. The next two were added by cron, exactly one minute apart, each at second 01. Nobody ran the script. Wait one more minute and there is one more line.
To watch new lines arrive as they are written, use tail -f cron.log and stop it with Ctrl + C.
Step 4: List and remove jobs
To see what is scheduled for your user:
crontab -l# m h dom mon dow command
* * * * * /home/ubuntu/cron-demo.shTo remove the schedule:
crontab -rcrontab -lno crontab for ubuntucrontab -r removes everything
crontab -r does not remove one job. It deletes your whole cron table at once and does not ask for confirmation. On a server with twenty scheduled jobs, that is twenty jobs gone. To remove a single job, use crontab -e and delete only that line. The keys e and r sit next to each other on the keyboard, so type this command slowly. Keeping a copy with crontab -l > crontab-backup.txt before you make changes is a good habit.
| Command | What it does |
|---|---|
crontab -e | Edit your cron table |
crontab -l | List your cron jobs |
crontab -r | Remove your whole cron table |
sudo crontab -e | Edit the cron table of root, for jobs that need root permission |
sudo crontab -u raj -l | List the jobs of another user |
Each user has a separate cron table, and a job runs with the permissions of the user who owns it.
Why a job works by hand but not in cron
This is the most common cron problem, and the cause is nearly always one of these.
Relative paths. Cron does not start in your scripts folder. It starts in your home directory. A script that writes to backups/ or reads project/ will look in the wrong place. Use full paths in the crontab line, as the lecture does, and inside the script too. Or move to the right folder first:
0 22 * * * cd /home/ubuntu/scripts && ./backup.shA very small environment. Cron does not read your .bashrc. The variables you exported there do not exist in a cron job, and PATH is much shorter than in your terminal. A command that works when you type it may be "not found" under cron. Use the full path of the command, which which <command> shows you.
The output is invisible. There is no screen for a cron job to print on. The second echo in cron-demo.sh goes nowhere. To keep the output and the errors of a job, redirect both into a file:
0 22 * * * /home/ubuntu/scripts/backup.sh >> /home/ubuntu/backup.log 2>&1>> appends the normal output to the file, and 2>&1 sends the errors to the same place. When a job misbehaves, this file is the first thing to read.
No execute permission. Cron runs the file directly, so it needs chmod +x, the same as when you run it with ./.
The clock. Cron uses the time of the server. Cloud servers normally run on UTC, as you can see in the log above. A job set for 0 22 runs at 10 PM UTC, which is 3:30 AM in India. Check with the date command before you choose the hour.
To confirm that cron really started your job, look at the system log:
grep CRON /var/log/syslog | tailCheck your expression before you save it
A cron expression is easy to get wrong and nothing tells you until the job runs at the wrong time, or not at all. The site crontab.guru translates an expression into a plain sentence. Paste yours there first.
Try It Yourself
- Write down the cron expression for each of these, then check them on crontab.guru: every day at 7:30 AM, every 10 minutes, every Saturday at 11 PM, and at 9 AM on the first day of every month.
- Create
cron-demo.sh, run it by hand and checkcron.log. - Schedule it for every minute with
crontab -e, wait three minutes and count the lines incron.log. - Change the schedule to every 2 minutes with
*/2and confirm it with the timestamps in the log. - Add
>> /home/ubuntu/cron-output.log 2>&1to the crontab line. After the next run, check that the "Log entry added" message is now in that file. - Save your jobs with
crontab -l > crontab-backup.txt, remove them withcrontab -r, and confirm withcrontab -l. A minute-by-minute job should not be left running on a practice machine.
Official references
Written By: Shramik Masti
How is this guide?
Last updated on
Auto Backup, System Health Check and Service Monitor
Three small automation scripts in Bash, a timestamped backup, a system health report and a service status checker built with systemctl.
Service Restart and Cleanup Scripts
Write a Bash script that restarts a service when it is down, and a cleanup script that deletes backup files older than seven days with find.
