Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Crontab entry for Perl script

by HitMan (Initiate)
on Aug 14, 2001 at 13:29 UTC ( #104710=perlquestion: print w/replies, xml ) Need Help??

HitMan has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've a Perl script that has to be run thru a Crontab (Sun solaris) entry.There are 2 problems I'm facing when run from cron: 1. The script is not picking up $ENV{VAR} value properly. I've to hard code the values for that , which is not what I want. 2. After harcoding the $ENV values the script runs to the point just before a call to DBI->connect , just on that statement it stops and does nothing , no error nothing .I'm at a loss to figure out the problem. When I'm running the same script from another Unix box, the 2nd problem is not there . I have a feeling it has something to do with the cron setup or cron env variables on this m/c. The script runs happily from command line. Any suggestions would be helpful. Thanx in advance.

Replies are listed 'Best First'.
Re: Crontab entry for a Perl script
by davorg (Chancellor) on Aug 14, 2001 at 13:30 UTC

    This is a cron question rather than a Pelr question.

    When a cron job runs, by default it doesn't pick up your environment variables. If you want the script to run in your usual environment (and it sounds like that might well be your problem) then you'll need to explicitly run your .profile. I generally do it like this:

    . /home/dave/.profile && script.pl
    --
    <http://www.dave.org.uk>

    Perl Training in the UK <http://www.iterative-software.com>

Re: Crontab entry for a Perl script
by Graham (Deacon) on Aug 14, 2001 at 13:26 UTC
    crontab invokes a shell from your home directory with an arg0 of sh. If you need to set up your environment then run the .profile (or whatever your particular shell uses) as part of the cron statement.
    I run the following from cron to successfully talk to an Informix database via DBI -
    #Informix server processes 0 7 * * 1-5 . /.profile;. /usr/local/lib/dir_locations;. $INFORMIXDIR/ +lib/environ.ksh;$SERVERDIR/sbin/main_server.pl > /dev/null 2>&1
    The .profile sets my environment.
    The file dir_locations sets common directories for all users such as INFORMIXDIR, extensions to $PATH.
    The file environ.ksh sets the required informix settings
    main_server.pl is the perl script that starts the whole ball rolling.

    Hope that is of some help

    Graham

Re: Crontab entry for Perl script
by htoug (Deacon) on Aug 14, 2001 at 14:43 UTC
    This is a quite normal problem when running anything from cron:
    You don't get your normal environment setup for you.

    You will have to either add the neccessary parts of your environment in your script by hand, or ensure that /etc/profile and ~/.profile (or whatever the local names are in your preferred OS) are sourced before you execute anything in your cron jobs.

    YMMV.

Re: Crontab entry for Perl script
by echo (Pilgrim) on Aug 14, 2001 at 16:27 UTC
    On (at least some variants of) Unix you can set up environment variables at the top of your crontab file:
    % crontab -l
    PERL5LIB=/home/echo/modules
    1 * * * * /home/echo/script.pl
    
Re: Crontab entry for Perl script
by traveler (Parson) on Aug 14, 2001 at 18:44 UTC
    As noted, (at least) Vixie's cron allows you to set environment variables. Some crons allow you to use the command field to set variables, thus:
    ENV1=val ENV2=val command
    Also, note that when you run from the command line your script has an open STDIN and an open STDOUT. In cron you do not have an open STDIN. STDOUT is generally a mail program. It is possible that cron errors are going to root. If you are not the admin, ask someone who is to see where cron errors go and if your script got any.

    HTH, --traveler

Re: Crontab entry for Perl script
by wardk (Deacon) on Aug 14, 2001 at 22:21 UTC

    thoughts about running from cron...

    • you need to setup environment assuming you inherit nothing from cron or your home .profile/.login/etc.
    • open a log file and write to it, so you have an audit trail, what I have done is created a standard log routine that I feed a message to, it writes out the time and the message. This is handy for scripts that serially work though a process, I when each step started/ended, and what happened. One thing you could do for debugging is dump your %ENV hash out to verify environment.

      Since this executes fine from the command line, once you get your cron ENV setup to mimic your login environment, you should be good to go.

      good luck!

Re: Crontab entry for a Perl script
by HitMan (Initiate) on Aug 14, 2001 at 14:22 UTC
    Thanx a lot that tip did work. I wrote a wrapper to setup all the environment variables and then ran the script and that worked. Thanx a lot guys.
Re: Crontab entry for Perl script
by Aighearach (Initiate) on Aug 14, 2001 at 14:26 UTC
    If you post your code you are more likely to get useful replies.
    --
    Snazzy tagline here

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://104710]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2022-05-28 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (99 votes). Check out past polls.

    Notices?