crontab in linux crontab tutorial

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.
crontab in linux is useful when we want some task to be run at particular time or at some interval of time period, 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 at 2:05 am on every day.

05 02 * * * /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

# Running script at 1:05 pm then we can use this line
05 13 * * * /opt/backup.sh

understanding 5 * at beginning of cron line.

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=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

Some more Example :

If we want to run script on one minute past every hour between 8:01 a.m. and 6:01 p.m.

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

/etc/cron.d
/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 mount.sh script will run.


You May Also Enjoy Reading This …

Leave a Reply

Your email address will not be published. Required fields are marked *