Imagine having a Bash script that backs up your files every day. The script works perfectly, but there is one problem: you have to remember to run it yourself.
What if Linux could run it automatically at a specific time, even when you are busy doing something else?
That is exactly what cron jobs are designed to do.
Cron is a time-based job scheduler available on many Linux and Unix-like systems. It allows you to schedule commands and scripts to run automatically at specified times or intervals.
In this article, we will explore how cron works, how to schedule tasks, and how to troubleshoot common problems.
1. What is a cron job?
A cron job is a scheduled command or task that runs automatically according to a defined schedule.
For example, you might want to:
- Back up important files every night.
- Run a maintenance script every Sunday.
- Generate a report every morning.
- Clean up temporary files regularly.
Instead of running these tasks manually, you can configure cron to execute them for you.
Cron uses a scheduling configuration called a crontab, short for cron table.
To open your personal crontab, run:
crontab -e
The first time you run this command, your system may ask you to choose a text editor. Once the editor opens, you can add your scheduled commands.
To view your existing scheduled jobs, use:
crontab -l
To remove your personal crontab entirely, use:
crontab -r
Be careful with the last command: it removes all jobs in your personal crontab, not just one entry.
2. Understanding cron syntax
A typical cron entry contains five time fields followed by the command to execute.
* * * * * command
Each asterisk represents a scheduling field.
| Field | Allowed values | Meaning |
|---|---|---|
| Minute | 0–59 | Minute of the hour |
| Hour | 0–23 | Hour of the day |
| Day of month | 1–31 | Calendar day |
| Month | 1–12 | Month of the year |
| Day of week | 0–7 | Day of the week; 0 and 7 usually mean Sunday |
Let's look at a real example:
0 8 * * * /home/user/scripts/backup.sh
This schedules backup.sh to run every day at 8:00 AM.
Why?
-
0means minute zero. -
8means 8 AM. - The remaining three asterisks mean every day of the month, every month, and every day of the week.
The command after the five fields is what cron executes.
Important: Cron generally uses the machine's configured local time zone unless configured otherwise. Check your system's time zone when scheduling important tasks.
3. Common cron expressions you should know
Once you understand the five fields, reading cron expressions becomes much easier.
* * * * * command
Runs every minute.
*/5 * * * * command
Runs every five minutes.
0 9 * * 1-5 command
Runs at 9:00 AM, Monday through Friday.
0 0 * * 0 command
Runs at midnight every Sunday.
30 18 * * * command
Runs every day at 6:30 PM.
The */5 expression means every five units within that field, while 1-5 represents a range of values.
One detail worth remembering: when both the day-of-month and day-of-week fields are restricted, traditional cron implementations generally run the job when either field matches. If you need a complicated schedule, check the documentation for your system's cron implementation.
4. Scheduling your first Bash script
Let's create a simple script and schedule it.
First, create a directory for your scripts:
mkdir -p "$HOME/scripts"
Create a file named daily-task.sh:
nano "$HOME/scripts/daily-task.sh"
Add the following code:
#!/bin/bash
echo "Cron ran at $(date)" >> "$HOME/cron-log.txt"
This script appends the current date and time to a log file whenever it runs.
Make the script executable:
chmod +x "$HOME/scripts/daily-task.sh"
Test it manually:
"$HOME/scripts/daily-task.sh"
Then inspect the log:
cat "$HOME/cron-log.txt"
If everything works, you are ready to schedule it.
Open your crontab:
crontab -e
Add this entry:
*/5 * * * * /home/yourusername/scripts/daily-task.sh
Replace /home/yourusername with your actual home directory path.
This runs the script every five minutes. After several minutes, inspect the log again:
cat "$HOME/cron-log.txt"
You should see additional timestamps.
And viola! You have created a Bash script and configured Linux to execute it automatically.
5. Why do cron jobs sometimes fail?
A script can work perfectly in your terminal and still fail when executed by cron.
Here are some common reasons.
Using relative paths
Your terminal might be in the directory containing your script, but cron may run it with a different working directory.
Instead of:
*/5 * * * * ./backup.sh
Prefer an absolute path:
*/5 * * * * /home/yourusername/scripts/backup.sh
Assuming your terminal environment is available
Cron usually provides a smaller environment than an interactive shell. Variables defined in your .bashrc may not be available.
If your script depends on environment variables, define the necessary ones explicitly or load the appropriate configuration deliberately.
Forgetting permissions
Check that the script exists and has the required permissions:
ls -l "$HOME/scripts/daily-task.sh"
Ignoring output and errors
Redirecting output to a log can make failures easier to investigate.
For example:
*/5 * * * * /home/yourusername/scripts/daily-task.sh >> /home/yourusername/cron-output.log 2>&1
The >> operator appends standard output to the log file, while 2>&1 sends standard error to the same destination.
For system-level troubleshooting, the available logs and service commands depend on your Linux distribution and cron implementation.
6. Cron vs. running a script manually
The difference is simple:
- Manual execution: You decide when to run the command.
- Cron: You define a schedule, and the system runs the command automatically when the schedule matches.
Cron does not make a script more powerful. It makes recurring execution more convenient.
You still need to write a correct script, handle errors, and ensure that its dependencies are available.
Final thoughts
Cron is one of the simplest ways to introduce automation into your Linux workflow. A few lines in a crontab can turn a manually repeated task into a scheduled process.
Start with something small, such as writing timestamps to a log. Then experiment with daily reports, backups, and other tasks that benefit from regular execution.
As you become more comfortable with Bash, cron will help you move from simply running commands to building repeatable workflows.
The real benefit of automation is not just saving time. It is making sure important tasks happen consistently, without depending on your memory.
Top comments (0)