An experienced Linux sysadmin knows the importance of running the routine maintenance jobs in the background automatically.
Linux Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going basis.
Linux Crontab Format
MIN HOUR DOM MON DOW CMD
MIN = Minute field = 0 to 59
HOUR= Hour field = 0 to 23
DOM = Day of Month = 1-31
MON = Month field = 1-12
DOW = Day of Week = 0-6
CMD = Command = Any command to be executed.
crontab -e to edit crontab file
crontab -l to display crontab file
crontab -r to remove crontab file
crontab -v to display the last time you edited your crontab file.
crontab -u used in conjunction with other options, this option allows you to modify or view the crontab file of user, when available, only administrator can use this option.
Example:-
vi task.sh
cal >> /home/oracle/test1.txt
:wq
Linux Cron utility is an effective way to schedule a routine background job at a specific time and/or day on an on-going basis.
Linux Crontab Format
MIN HOUR DOM MON DOW CMD
MIN = Minute field = 0 to 59
HOUR= Hour field = 0 to 23
DOM = Day of Month = 1-31
MON = Month field = 1-12
DOW = Day of Week = 0-6
CMD = Command = Any command to be executed.
crontab -e to edit crontab file
crontab -l to display crontab file
crontab -r to remove crontab file
crontab -v to display the last time you edited your crontab file.
crontab -u used in conjunction with other options, this option allows you to modify or view the crontab file of user, when available, only administrator can use this option.
Example:-
vi task.sh
cal >> /home/oracle/test1.txt
:wq
make sure script has all the permissions
chmod 777 task.sh
we are going to run the task every minute
now go to the crontab
$crontab -e
* * * * * /home/oracle/task.sh
:wq
1. Scheduling a Job For a Specific Time
30 08 10 06 * /home/ramesh/full-backup
8-30 am
10th day
6th month (june)
* every day of the week
2. Schedule a Job For More Than One Instance (e.g. Twice a Day)
The following script take an incremental backup twice a day every day.
00 11,16 * * * /home/oracle/incremental-backup
00 = 0th minute
11,16 = 11 am, 4pm
* = every day
* = every month
* = every day of the week
3. Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)
Cron Job everyday during working hours
00 09-18 * * * /home/ramesh/bin/check-db-status
00 = 0th minute
09-18 = 9am, 10am, 11am, 12am, 1pm, 2pm, 3pm, 4pm, 5pm, 6pm
* = every day
* = every month
* = every day of the week
00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
00 = 0th minute
09-18 = 9am, 10am, 11am, 12am, 1pm, 2pm, 3pm, 4pm, 5pm, 6pm
* = every day
* = every month
1-5 = mon, tue, wed, thu and fri (working days)
4. How to View Crontab Entries?
$crontab -l
To view Root user Crontab entries
# crontab -l
To view other linux user's crontab entries
{username} -l
[root@dba ~]# crontab -u oracle -l
* * * * * /home/oracle/task.sh
5. How to Edit Crontab Entries?
$crontab -e
Edit Root Crontab entries
root@dba-db# crontab -e
To edit crontab entries of other Linux users, login to root and use -u
root@dba-db# crontab -u oracle -e
6. Schedule a Job for Every Minute Using Crontab.
* * * * * CMD
when we specify */5 in minute field means every 5 minutes
1. Scheduling a Job For a Specific Time
30 08 10 06 * /home/ramesh/full-backup
8-30 am
10th day
6th month (june)
* every day of the week
2. Schedule a Job For More Than One Instance (e.g. Twice a Day)
The following script take an incremental backup twice a day every day.
00 11,16 * * * /home/oracle/incremental-backup
00 = 0th minute
11,16 = 11 am, 4pm
* = every day
* = every month
* = every day of the week
3. Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)
Cron Job everyday during working hours
00 09-18 * * * /home/ramesh/bin/check-db-status
00 = 0th minute
09-18 = 9am, 10am, 11am, 12am, 1pm, 2pm, 3pm, 4pm, 5pm, 6pm
* = every day
* = every month
* = every day of the week
00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
00 = 0th minute
09-18 = 9am, 10am, 11am, 12am, 1pm, 2pm, 3pm, 4pm, 5pm, 6pm
* = every day
* = every month
1-5 = mon, tue, wed, thu and fri (working days)
4. How to View Crontab Entries?
$crontab -l
To view Root user Crontab entries
# crontab -l
To view other linux user's crontab entries
{username} -l
[root@dba ~]# crontab -u oracle -l
* * * * * /home/oracle/task.sh
5. How to Edit Crontab Entries?
$crontab -e
Edit Root Crontab entries
root@dba-db# crontab -e
To edit crontab entries of other Linux users, login to root and use -u
root@dba-db# crontab -u oracle -e
6. Schedule a Job for Every Minute Using Crontab.
* * * * * CMD
when we specify */5 in minute field means every 5 minutes
7. Schedule a Background Cron Job For Every 10 Minutes.
*/10 * * * * /home/oracle/check-disk-space
Instead of specifying values in the 5 fields, we can specify it using a single
keyword as mentioned below.
There are special cases in which instead of the above 5 fields you can use @
followed by a keyword — such as reboot, midnight, yearly, hourly.
keyword equivalent
@yearly 0 0 1 1 *
@daily 0 0 * * *
@hourly 0 * * * *
@reboot run at startup
8. Schedule a Job For First Minute of Every Year using @yearly
@yearly /home/oracle/red-hat/bin/annual-maintenance
9. Schedule a Cron Job Beginning of Every Month using @monthly
@monthly /home/oracle/suse/bin/tape-backup
10. Schedule a Background Job Every Day using @daily
@daily /home/oracle/arch-linux/bin/cleanup-logs "day started"
11. How to Execute a Linux Command After Every Reboot using @reboot?
@reboot CMD
very good one ...
ReplyDelete