Using cron in Linux
cron is a time-based job scheduler in Unix-like operating systems. Users can schedule jobs (commands or scripts) to run at specific times or intervals. This guide will cover the basics of using cron, creating a simple bash script, and setting up cron jobs with various schedules.Creating a Simple Bash Script
First, let's create a simple bash script that we want to run periodically.- Open a terminal.
- Create a new file called my_script.sh:
Code:nano my_script.sh
- Add the following content to the script:
Code:#!/bin/bash echo "Hello, World! The current date and time is: $(date)" >> /path/to/logfile.log
- Save the file and exit the editor (Ctrl+X, then Y, then Enter).
- Make the script executable:
Code:chmod +x my_script.sh
Setting Up a Cron Job
To schedule the script to run every 4 hours, we need to edit the crontab file.- Open the crontab editor:
Code:crontab -e
- Add the following line to schedule the script to run every 4 hours:
Code:0 */4 * * * /path/to/my_script.sh
Understanding the Crontab Command
The crontab command is used to manage cron jobs. Here are some useful flags:-
Code:
-e
-
Code:
-l
-
Code:
-r
Viewing Crontabs for Other Users
To view or edit the crontab for another user, the root user can use the following command:
Code:
crontab -u username -e
Examples of Cron Job Schedules
Here are some examples of different cron job schedules:- Run a script every day at 2:30 AM:
Code:30 2 * * * /path/to/my_script.sh
- Run a script every Monday at 5:00 PM:
Code:0 17 * * 1 /path/to/my_script.sh
- Run a script every 15 minutes:
Code:*/15 * * * * /path/to/my_script.sh
Detailed Explanation of Cron Job Fields
In a cron job schedule, the asterisks (*) represent different time fields. Each field specifies a particular time unit, and the asterisks act as wildcards, meaning "every" possible value for that unit. Here's a detailed breakdown:* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of the week (0 - 7) (Sunday is both 0 and 7)
| | | +------- month (1 - 12)
| | +--------- day of the month (1 - 31)
| +----------- hour (0 - 23)
+------------- minute (0 - 59)
Explanation of Each Field
- Minute: The first field represents the minute of the hour. It can range from 0 to 59.
- Example: 0 means the job will run at the start of the hour.
- Hour: The second field represents the hour of the day. It can range from 0 to 23.
- Example: 2 means the job will run at 2 AM.
- Day of the Month: The third field represents the day of the month. It can range from 1 to 31.
- Example: 15 means the job will run on the 15th of the month.
- Month: The fourth field represents the month of the year. It can range from 1 to 12.
- Example: 7 means the job will run in July.
- Day of the Week: The fifth field represents the day of the week. It can range from 0 to 7, where both 0 and 7 represent Sunday.
- Example: 1 means the job will run on Monday.
Using Asterisks
When you use an asterisk in any of these fields, it means "every" possible value for that field. For example:- * * * * * means the command will run every minute of every hour of every day of every month.
- 0 * * * * means the command will run at the start of every hour.
- 0 0 * * * means the command will run at midnight every day.
- 0 0 1 * * means the command will run at midnight on the first day of every month.
- 0 0 * * 0 means the command will run at midnight every Sunday.
Combining Values
You can also combine values using commas, hyphens, and slashes:- Commas: Separate multiple values. Example: 0,15,30,45 * * * * runs at 0, 15, 30, and 45 minutes past the hour.
- Hyphens: Specify a range of values. Example: 0-5 * * * * runs at minutes 0, 1, 2, 3, 4, and 5.
- Slashes: Specify step values. Example: */10 * * * * runs every 10 minutes.
The /var/spool/cron Directory
The /var/spool/cron directory contains the crontab files for each user. Each file is named after the corresponding username. These files should not be edited directly; instead, use the crontab command to make changes.
Last edited: