Guide to add a cronjob - Simplified!
So, whats cronjob? Its basically used to run periodic tasks on linux. System administrators use it for making backups, log rotations, etc..
I will deal with dcron. Its the default that comes with Arch Linux, so I am going to stick with that. At the end of the post, I will add a script to cronjon that checks on a webpage for update and alerts the user.
So, to see the list of current cronjobs, you simply do
$crontab -l
Now, each user can has its own set of cronjobs. It all depends as to with what user previlidge, you are running crontab. On my machine, when I did $crontab -l, I just got an open text fill with nothing in it. Thats the case, all the cronjobs on my machine were running with root prevelidge.
So, as I said earlier, too see the cronjobs of root, you have to do:-
shadyabhi@ArchLinux ~ $ sudo crontab -l
Password:
# root crontab
# DO NOT EDIT THIS FILE MANUALLY! USE crontab -e INSTEAD
# man 1 crontab for acceptable formats:
#
# <@freq>
#SYSTEM DAILY/WEEKLY/... FOLDERS
@hourly ID=sys-hourly /usr/sbin/run-cron /etc/cron.hourly
@daily ID=sys-daily /usr/sbin/run-cron /etc/cron.daily
@weekly ID=sys-weekly /usr/sbin/run-cron /etc/cron.weekly
@monthly ID=sys-monthly /usr/sbin/run-cron /etc/cron.monthly
shadyabhi@ArchLinux ~ $
Basically an entry in crontab file looks like this:-
* * * * * /bin/execute/this/script.sh
And the stars(*) mean:-
1. every minute
2. of every hour
3. of every day of the month
4. of every month
5. and every day in the week.
Woah!!, but my file didnt look anythin like it. It has all this "@something" in the beginning and no *(stars). Thats because there is not only one way to describe a cronjob.
The formats like @hourly, @daily, @weekly, @monthly, @yearly, @reboot are also recognised which mean the obvious. So, my file says that run the command "/usr/sbin/run-cron /etc/cron.hourly" @hourly. The full path to the binary "run-cron" is given because these cronjobs dont really have a PATH variable defined. (well, it can be defined too, but why take risk). The argument to that command is a directory and all the commands in that directory are executed.
Suppose, you want to run a command every 30 minutes you will write the something like "30 * * * *" instead of @hourly.
Lets take an example:-
0 1 * * 1-5 /bin/execute/this/script.sh
says to run a command Mon-Friday at 1AM. That lines says that run that script.sh every 0minute,1hour,EVERY day of month, EVERY month, but only on weekdays 1 to 5. It will be run exactly at 01:00:01.
Another way that crontab provides for simplicity is
0,20,40 * * * * /bin/execute/this/script.sh is same as
*/20 * * * * /bin/execute/this/script.sh
The above lines are equivalent. They run the command at every 20 minutes of all days in any year.
One more example from the manpage says:-
# run every two hours between 11 pm and 7 am, and again at 8 am
0 23-7/2,8 * * * date
I dont think, I need to explain that.
So, what use it is? Well, it is very useful. Like in Arch Linux, we arch linux use it to create a Statistics page for all the arch users.
https://www.archlinux.de/?page=Statistics
This cronjob is added as soon as you install pkgstats.
Well, I will take one more example in which I use it. I study in a institute and all the mails that arrive for a student are collected by our hostel supervisor and then the status is posted on our intranet hostel website. So, we are forced to check it from time to time, and there is a chance that you can miss it because we dont have email notification for that. My roomie (Ranveer) gave me the idea of making a script that will check periodically for new mails. So, I fired up the terminal & decided of making a cronjob that will check for new entries added to the website and alert me with a GUI dialog as soon as I recieve the mail.
#!/bin/bash
ROOM_NO="G104"
echo `date`": " >> $HOME/.snailmailog
for pg in {1..10};
do
add_to_log=`curl -s "http://hostel.daiict.ac.in/index.php?option=com_eventtableedit&Itemid=2&limit=30&page=$pg" | html2text -nobs | grep $ROOM_NO`
if [ $? -eq 0 ];then
add_to_log=`echo $add_to_log | sed -e 's/^.*$ROOM_NO *//g' -e 's/ *//g' -e 's/^.//g'` #->1. grep ROOM_NO ->2.Removing SPACES ->3. Remove the first Serial number (To make it a unique entry"
grep "$add_to_log" $HOME/.snailmailog
if [ $? -eq 1 ]
then
kdialog --display :0 --title "MAIL RECIEVED" --msgbox "$ROOM_NO mail recieved!! Rush to the supervisor. "
fi
echo $add_to_log >> $HOME/.snailmailog
fi
done
I added to the cronjob with normal user and not root. So, I have only one entry for shadyabhi. This job executes every hour.
shadyabhi@ArchLinux ~/cronjobs $ crontab -l
#CronJobs located in $HOME/cronjobs/*
0 * * * * /home/shadyabhi/cronjobs/snailmail.sh