Cronjob to run perl script

The first thing I would suggest is to see if your Perl script is running fine. Normally what cron does is to email the output of the script or the errors  if any exists to the email address provided.

As  I wanted to run my script daily and already had an email function inside my script I did not use it although for simpler scripts you can use it. You just need to write a line at the top of crontab i.e.

MAILTO=”asdasd@ads.com”;

 

Now the main part i.e. running a Perl Script. Normally they require an environment variable to be set as cron does not execute in the same environment as your script does when you do it manually. There are many things you can do but the best thing that I found out was to add four lines at the top of your script

i.e.

#!/usr/local/bin/perl

BEGIN {

push @INC,(‘etc/perl

/usr/local/lib/perl/5.12.4,

/usr/lib/perl5,

/usr/share/perl5,

/usr/lib/perl/5.12,

/usr/share/perl/5.12’

);

}

 

This is basically telling where your Modules are situated and as cron does not know. You can just copy paste this as most of the time it is the same else just write the path of the folder where your Perl Modules are stored.

Now one thing you should check is try going out of all the folder and run your script giving the whole path and see if it is running or not i.e. ‘$ perl /home/admin/Desktop/1.pl’.

If it runs then you are ready to set up your cron Job else edit the paths that I have given with the ones that are shown in the error message and try again.

Now open a terminal and type ‘crontab –e’. This enables you to edit the crontab file.

Now if you are using UBUNTU you are given an option of using the NANO editor select it its easy and if you are using Fedora then you have to use ‘vi’. Don’t know learn it.

Now start typing the command

‘00 4 * * * perl /home/admin/Desktop/2.pl’

This command will run your perl Script every day at 4 in the morning. The * * * are different combinations of time day week month and year. You can easily lookup that on the internet.

TADA!!!!! Your cron job is ready to run.

http://www.itauthor.com/2010/08/03/gotchas-with-running-a-perl-script-as-a-cron-job/

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s