You are currently viewing Crontab tutorial in Linux

Crontab tutorial in Linux

Crontab tutorial in Linux

crontab tutorial in linux, crond is a daemon which run in background and enables cron task.cron check in every minutes for task in crontable.
when we want some task or process to be run at particular time or at some interval of time period then we used crontab, suppose if we want to run backup script every day at 2 am then we can add cron in crontab configuration file.

Entering in crontab

sudo su
export EDITOR=vi
crontab -e

Here first command is for entering in root account
then export EDITOR=vi which will open crontab file in vi editor
and then open crontab file

now we can enter cron in this file like i added below line which will run backup.sh (path is /opt/backup.sh) script everyday morning at 4:15 .

15 04 * * * /opt/backup.sh

then save and exit (Esc :wq!)


View crontab entries

$ sudo crontab -l

this will list all entries of cron


Editing crontab

$ sudo crontab -e

from this we can edit the crontab file

Also if we want to enter cron for specific user

Example

$ sudo crontab -u kencorner -l

this command will display cron for user kencorner.


crontab in linux

# If we want to running script at 2:05 pm then it should look like this,
05 14 * * * /opt/backup.sh

Now understanding each five star at the beginning of crontab..

First value : Minute (0-59)
Second value : Hour (0-23)
Third value : Day of Month (1-31)
Fourth value : Month (1 – 12) OR jan,feb,mar,apr
Fifth value : Day of week (0 – 6) (Sunday will be 0 or 7) OR also we can usedĀ  sun,mon etc.

Some more Example :

If we want to run script on one minute past every hour between 8:01 morning to 6:01 evening.

01 08-18 * * * /opt/backup.sh

Here script will run every three minutes (10:03, 10:06, 10:09, etc.) during even-numbered hours from 10 a.m. to 6 p.m.

*/3 010-18/2 * * * /opt/backup.sh

also we can run quarterly on the first day of the month

02 03 1 1,4,7,10 * /opt/backupscript.sh

This will runs one minute past every hour between 8:01 a.m. and 6:01 p.m.

01 08-18 * * * /opt/backupscript.sh


Predefined cron directory

some of predefined cron directory is as follow

/etc/cron.daily # it will run every day
/etc/cron.hourly # it will run every hour
/etc/cron.monthly # it will run every month
/etc/cron.weekly # it will run weekly


Special Strings which we can use in crontab file for scheduling task

@reboot run when the system reboot
@daily Once per day
@weekly Once per week
@yearly Once per year

Example

@reboot /opt/mount.sh

on every reboot of system it will runĀ  mount.sh script .


You May Also Enjoy Reading This …

Leave a Reply