Powered By Blogger

Sunday, June 7, 2009

cronjobs scheduling in Unix, Linux

I had to struggle a lot to find the exact working example for scheduling cron jobs in Unix.
Searched a lot in google and couldn't find the exact solution, after doing research here are the steps which not only gives complete working example, it even explains how to verify if the jobs have been executed successfully. Let's do step by step

Step1
-------
Create a perl script which prints "Hello world" say hello.pl in location /tmp/ by pasting the below contents

#!/usr/bin/perl
use strict;
use warnings;
print "Hello world\n";



Step2
---------
Before scheduling through cron job let us first verify manually
Execute the command:
/usr/bin/perl /home/hello.pl >> /tmp/log3.txt

The ouput is redirected to /tmp/log3.txt
Open log3.txt and observe the output "Hello world"

Step3
--------
First verify if there are already any cron jobs existing

command: crontab -l
the above command will list the cron jobs


Step4
-------
To add cronjob use the following command:
command: crontab -e
the above command opens the file and we need to add the following line

* * * * * /usr/bin/perl /home/hello.pl >> /tmp/log3.txt

Step5
------
After adding the above line, save the file . As soon as the file is saved and if there are no errors in the entries the cronjob will be saved and executed as described.
You should get a response as installing new crontab on saving the cron file) To list the cronjobs in your id, issue the command
crontab -l

the above command will list the newly added cronjob

Step6
-------
The above cronjob will execute the script(hello.pl) every minute. Check the /tmp/log3.txt at the end of 5 minutes and there should be 5 "Hello world" entries.

More information on timing
------------------------------
if there are 5 stars ( * * * * *) it executes everyminute. We can change this to appropriate date/time/month/year by following the below Rule
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed

Hope this example is useful for everyone.

Thursday, January 1, 2009

JSP

Struts

Sample questions