Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a perl script from the crontab

Status
Not open for further replies.

mow14

Technical User
May 12, 2008
4
IE
Hi guys,

I'm having an issue with running a perl script from the crontab. I've been looking into it for a while so have tried most of the suggestions i've come across on the net and still no luck. I've added the full perl path and path of the script to the cron entry, the permissions of the script are fine, the cron and the script are owned by the same user, there are no errors in the users mail relating to the cron entry for the script. I thought it may be that the cron was not picking up the correct environment variables as the user does initialise some of these from its profile but when i added these to a shell script which also called the perl script it still didn't work. I am calling an expect script inside the perl script, not sure if this would cause any issues? Needless to say it runs fine from the command line! Does anyone have any ideas as to what the problem might be?

Cheers!
 
In your description, there are many places where something can go wrong. I can deduce you are using Perl on a *nix operating system, but not much more. Please give us more information, such as:
Version of the operating system and hardware platform.
Version of perl
The exact 'crontab' entry for the script

Also,
is the cron daemon running?
what shell is the script running in?

I my experience of crontab, it is usually an 'environmental' issue.

I hope that helps.

Mike
 
what sort of errors are you getting in the log under /var/cron. This log mostly owned by root so you'll need that access, but will tell you why the cron is not running.

In the shell script, are defining each env var, or envoking the users profile via . /path/to/.profile (depends on which shell ur using).
 
Hi,

I'm running it on a sun platform, solaris 10 OS, perl 5.8. The entry i have in the cron is

00 18 * * * /usr/bin/perl /path/to/script

The /usr/bin/perl executable has a link to /usr/bin/perl5/5.8.4/bin/perl

I have two entries in the .profile, as below, for the user that may/may not be significant for this script. The perl script is also using a perl module that has been written and installed to site_perl, it is not part of the standard perl library.

export PATH=/opt/oracle/bin:/opt/oracle/product/10.2.0.2/bin:/usr/sbin:/usr/bin:/app01/ilink/sas/bin:/opt/oracle/product/10.2
.0.2/lib32:/usr/cluster/bin:/usr/local/bin:/usr/perl5/site_perl/5.8.4

export PERL5LIB=/usr/perl5/site_perl/5.8.4

Cheers




 
Don't expect anything in the .profile to affect the cron when it runs.
If you need oracle paths I would suggest exporting them in your script.
Also full path everything (even things like ping).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I had to add this to get Oracle working in my cron scripts
$ENV{'ORACLE_HOME'} = '/global/oracle/product/9.2.0.1.0';

and my connect line for reference.
$dbh = DBI->connect('DBI:Oracle:host=testhost;sid=TESTSID', 'TESTUSER', 'TESTPAS') or die "Can't connect to DB: $DBI::errstr\n";
$dbh->{LongReadLen} = 65535;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I have this in another script where I am remshing over to a oracle machine and need to get the env variable set. Definitely written when I first started learning so there is definitely better to use single quotes or something :)

#Var's needed for dbi query in oracle
$envvar = "export SHLIB_PATH=/oracle/product/8.1.7/lib\\\;export LD_LIBRARY_PATH
=/oracle/product/8.1.7/lib\\\;";

@data = `remsh server -l user $envvar/home/user/SCRIPTS/script.pl @ser`;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Have you tried writing some debug out to a logfile. Is it that the script doesn't run at all, or it definitely runs, but does jack and craps out, or what seems to be the issue?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Hi mow14,

Thanks for the info.
First of all, the .profile is not run from a cron job (it 's only used once at login time). Second, cron is very basic and runs jobs in a Bourne Shell. If the job produces any output on STDOUT or STDERR then that is sent to the user via e-mail.

I suspect what is happening is that cron is not passing the scriptname (at parameter 7) to /usr/bin/perl (at 6pm every day) so perl just exits as it has nothing to run and there is no output - hence no e-mail.

Here is what I would do:

Change to crontab entry to:
00 18 * * * /path/to/calling_script

Create a wrapper (or calling script) as:
Code:
#!/bin/sh
#####################################################
###  Script:    calling_script
###  Location:  /path/to
###  Description:
###  The calling script for: /path/to/script
#####################################################
   . ${HOME}/.profile

   /usr/bin/perl /path/to/script

Note that HOME is one of about 5 environmental variables that is available when running cron jobs.

I hope that helps.

Mike
 
I have never had to wrap perl scripts in cron that way (just put the path to the script and any variable I want to pass to it), but I sure don't depend on anything in the profile either :)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top