Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 8 Aug 2002 19:21:31 +0100
From:      Matthew Seaman <m.seaman@infracaninophile.co.uk>
To:        Cliff <cliff@travelguides.com>
Cc:        freebsd-questions@FreeBSD.ORG
Subject:   Re: Cron running scripts
Message-ID:  <20020808182131.GB82086@happy-idiot-talk.infracaninophi>
In-Reply-To: <009201c23f5d$a48911b0$6401a8c0@tsunami>
References:  <009201c23f5d$a48911b0$6401a8c0@tsunami>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Aug 08, 2002 at 09:31:33PM -0700, Cliff wrote:
> I have a perl script that is run every night to backup some directories. The
> output from the scripts says it completed fine, but nothing is actually
> output. When I run the script manually it produces the same output, but it
> actually writes the backup files. Why doesn't cron running the script do
> this?

Failure of cronjobs in this way is usually because the script author
has assumed that the cronjob runs with the same environment as their
usual login session.  That is certainly not the case.  You can see
what environment cron supplies to any jobs by inserting a line:

    * * * * *    env > /tmp/cron-environment

into the crontab (use 'crontab -e' to do that), and waiting for a
minute.  As that command will be run every minute, take it out again
PDQ.  The result will be like this:

    happy-idiot-talk:~:% cat /tmp/cron-environment 
    USER=matthew
    HOME=/home/matthew
    LOGNAME=matthew
    PATH=/usr/bin:/bin
    SHELL=/bin/sh

As for why your particular script failed, it's impossible to say
without actually seeing the script.  As a general principle though,
always set the PATH to something useful and/or use the full path of
any command you may run.  Test the return value from commands run via
`backticks` or system:

    $ENV{PATH} = "/bin:/usr/bin:/usr/local/bin";

    $output = `command`;
    die "command failed: $?" if $?;

    @args = ("/usr/bin/command", "arg1", "arg2", "arg3"); 
    system(@args) == 0
	or die "system @args failed: $?"

Try and avoid issuing the command to system() as a single string
containing shell metacharacters if you can --- perl will automatically
fire up the shell to run the command for you, and that can obscure the
exit status of your command.

	Cheers,

	Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.                       26 The Paddocks
                                                      Savill Way
Tel: +44 1628 476614                                  Marlow
Fax: +44 0870 0522645                                 Bucks., SL7 1TH UK

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020808182131.GB82086>