CRONTAB Basic
Basics of Cron Tab (Crontab)
Crontab (short for "cron table") is a configuration file in Unix-like operating systems used to schedule jobs (commands or scripts) to run at specific times or intervals. The cron daemon runs in the background and checks the crontab file for jobs to execute.
1. Understanding Crontab Syntax
Each line in a crontab file represents a job and follows this format:
scss
Copy code
* * * * * /path/to/command
- - - - -
| | | | |
| | | | |
| | | | └─── Day of the week (0-6) (Sunday=0 or 7)
| | | └───── Month (1-12)
| | └─────── Day of the month (1-31)
| └───────── Hour (0-23)
└─────────── Minute (0-59)
Minute: Specifies the minute (0-59) when the job should run.
Hour: Specifies the hour (0-23) when the job should run.
Day of the Month: Specifies the day of the month (1-31) when the job should run.
Month: Specifies the month (1-12) when the job should run.
Day of the Week: Specifies the day of the week (0-6, where 0 = Sunday) when the job should run.
You can use the following symbols to control the schedule:
*: Any value (i.e., all possible values for that field).
,: Separate multiple values (e.g., 1,15 means run on the 1st and 15th).
-: Specify a range (e.g., 1-5 means run from the 1st to the 5th).
/n: Specify steps (e.g., */5 means every 5 minutes).
2. Common Examples of Crontab Scheduling
Here are some common crontab expressions:
a. Run a Job Every Minute
bash
Copy code
* * * * * /path/to/command
b. Run a Job at 2:30 AM Every Day
bash
Copy code
30 2 * * * /path/to/command
c. Run a Job Every Hour
bash
Copy code
0 * * * * /path/to/command
d. Run a Job Every Day at Midnight
bash
Copy code
0 0 * * * /path/to/command
e. Run a Job Every Sunday at 5 PM
bash
Copy code
0 17 * * 0 /path/to/command
f. Run a Job on the 1st of Every Month at 12:00 PM
bash
Copy code
0 12 1 * * /path/to/command
g. Run a Job Every 10 Minutes
bash
Copy code
*/10 * * * * /path/to/command
h. Run a Job from Monday to Friday at 3:00 AM
bash
Copy code
0 3 * * 1-5 /path/to/command
3. Viewing and Editing Crontab
To interact with your crontab file, you use the following commands:
a. Edit Crontab
To edit the crontab for the current user:
bash
Copy code
$ crontab -e
This opens the crontab file in the default text editor (usually vi or nano), where you can add, edit, or remove jobs.
b. List Crontab Entries
To view the existing crontab jobs for the current user:
bash
Copy code
$ crontab -l
c. Remove All Crontab Entries
To remove all crontab jobs for the current user:
bash
Copy code
$ crontab -r
d. Edit Crontab for Another User
To edit the crontab of a different user (requires root privileges):
bash
Copy code
$ sudo crontab -u username -e
4. Redirecting Output
By default, cron sends the output (both stdout and stderr) to the user's email. If you want to redirect output to a file, you can do it like this:
bash
Copy code
* * * * * /path/to/command >> /path/to/logfile.log 2>&1
>> /path/to/logfile.log: Redirect standard output (stdout) to a log file.
2>&1: Redirect standard error (stderr) to the same log file.
5. Cron Special Keywords
Instead of specifying the exact time for common schedules, you can use cron's special keywords:
Keyword Equivalent to Example
@reboot
Run once at boot
@reboot /path/to/command
@yearly
0 0 1 1 *
Run once a year at midnight on January 1st
@annually
Same as @yearly
@monthly
0 0 1 * *
Run once a month at midnight on the 1st
@weekly
0 0 * * 0
Run once a week at midnight on Sunday
@daily
0 0 * * *
Run once a day at midnight
@midnight
Same as @daily
@hourly
0 * * * *
Run once an hour at the beginning of the hour
6. Crontab Example: Run a Script Daily at 1 AM
To run a backup script daily at 1 AM, follow these steps:
Open the crontab editor:
bash
Copy code
$ crontab -e
Add the following line:
bash
Copy code
0 1 * * * /path/to/backup_script.sh >> /path/to/backup_log.txt 2>&1
Save and exit the crontab.
This will execute the backup_script.sh at 1:00 AM every day, and the output will be logged in backup_log.txt.
7. Useful Cron Commands
Check cron status (on most Linux systems):
bash
Copy code
$ systemctl status cron
Restart cron:
bash
Copy code
$ sudo systemctl restart cron
8. Crontab User Permissions
Only users listed in /etc/cron.allow are allowed to use cron, and users listed in /etc/cron.deny are denied access. If neither file exists, then cron is generally available to all users.