'crontab' | JOB SCHEDULING COMMAND IN LINUX



Crontab is command line utility which helps the system admin to schedule their jobs which can be repeated periodically. 

In order to check the package you have installed or not, just run below command

rpmquery -qa | grep cront*

output will looks like 












cronie-anacron is an another package which also required in order to execute crontab schedule jobs : read man page about more details.

1) To edit the crontab file to schedule jobs:

crontab -e

and then you will get a vim editor where you can schedule any job for any repeated time.










here I have schedule a job to check the disk space of every mounted partitions and send them to /var/log/diskutilization.txt file. 

you can understand the time format which I used to scheduled for my task in below picture











first value is for minutes : (60 minutes format)
second value is for hours:(24 hours format)
third value is for day of the month: (1,2,3,4...,30,31)
fourth value is for month : (jan,feb,mar..or 1 - 12 ) 
fifth value is for week days: ( sun,mon,tue.... 0-6 )

so this way my job will be executed on "At 12:00 on day-of-month 1 and on Monday

Note: 

>>If you want to repeat the task frequently (e.g every 5th minute). 

*/5 12 01 jan sun df -h > /home/usage.txt

This will repeat your task  "At every 5th minute past hour 12 on day-of-month 1 and on Sunday in January"

>> if you want to repeat the task frequently in hours ( e.g every 5th hour)

1 */5 01 jan sun df -h > /home/usage.txt

This will repeat your task "At minute 1 past every 5th hour on day-of-month 1 and on Sunday in January"

>> If you want to specify the range of days or month you can do that also

00 5 1-10 1-3 * df -h > /home/usage.txt

This will execute your task "At 05:00 on every day-of-month from 1 through 10 in every month from January through March"

2) If you want to schedule a job for any other user just edit that command  with user name option. see below pic

crontab -u 'user_name' -e

3) If you want to check the list of scheduled jobs 

crontab -l


4) If you want to remove the scheduled jobs

crontab -r

5) You can specify your scripts or jobs in side the /etc/ cron.* directories 
which specified for weekly daily and hourly tasks. just put your scripts inside that folder and they will run on respected basis







6) What if you have multiple jobs to schedule & might you have to edit them after some time for time and day . so just edit  your jobs in txt file and then run them on crontab.












Then run this command crontab /path_to_your_txt_file



You can use visit www.crontab.guru to schedule your jobs. 






Post a Comment

0 Comments