Categories
commands

Crontab !!

Contents

Crontab Basics

crontab is most powerful yet very simple.this little tool is used to set or schedule periodic or one time jobs in linux..

it is not that complicated..

Here is a summary of commands which are used to configure jobs with crontab

crontab -e (edit user’s crontab)
crontab -l (list user’s crontab)
crontab -r (delete user’s crontab)
crontab -i (prompt before deleting user’s crontab)

to create a new periodic or schedule task you need to edit the crontab file .
enter

$crontab -e

the configuration file will be opened in nano

ok these are the fields you have to fill.. any way

# m h dom mon dow command

m —— minute (0-59)
h —— hour (0 to 23)
dom —— day of month (1to31)
mon —— month (1-12)
dow —— day of week (0 to 6 , 0 –> sunday )
command ——- program to be run

note:- all fields are mandatory
Go to Top

Alarm

lets write a script that plays an audio file on evoking it..
like below

#!/bin/sh
mplayer audiofile.mp3

lets save this as file alarm

as owner of file you can make the file executable by entering

$ chmod a+x alarm

ps: both file alarm & audiofile.mp3 must be in same directory

ok now you want it to ring at every morning 7:45
Then here is the line here you have to enter

# m h dom mon dow command
45 7 * * * ~/alarm

you have an alarm now free of cost!!

Other utilities?
Go to Top

Upgrading System

take this script for example;

#!/bin/sh
apt-get update
apt-get -y upgrade
apt-get clean
apt-get autoclean

save above script in auto-upgrade.sh make it an executable like before and save it in /root directory

now i set my pc’s root crontab as

# m h dom mon dow command
00 00 * * 6 /root/auto-upgrade.sh

You can see my auto upgrade program runs every saturday at midnight and keep my system uptodate!!
Go to Top
if you can mix shell scripting and crontab you do a lot of cool things..

Website Backup

you can also take a periodic backup of your website using wget and crontab.

#!/bin/sh
dir=`date -d now +%d-%m-%y`
mkdir $dir
cd $dir
wget -r http://link-to-your-website

save it as websitebackup.sh

and set it in crontab to every sunday night

# m h dom mon dow command
00 00 * * 0 /home/myuserid/websitebackup.sh

well you have your website weekly backup now for you.!!
any way you can see now how powerful this simple tool can be.

You can also schedule a periodic back up of your important files in the above manner.
Go to Top
References

crontab command manual
wget command manual
date command manual
apt-get manual