From owner-freebsd-questions Thu Aug 8 11:21:49 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.FreeBSD.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4604237B48B for ; Thu, 8 Aug 2002 11:21:38 -0700 (PDT) Received: from smtp.infracaninophile.co.uk (happy-idiot-talk.infracaninophile.co.uk [81.2.69.218]) by mx1.FreeBSD.org (Postfix) with ESMTP id 412DA43E6E for ; Thu, 8 Aug 2002 11:21:37 -0700 (PDT) (envelope-from m.seaman@infracaninophile.co.uk) Received: from happy-idiot-talk.infracaninophile.co.uk ([IPv6:::1]) by smtp.infracaninophile.co.uk (8.12.5/8.12.5) with ESMTP id g78ILaFo083564; Thu, 8 Aug 2002 19:21:36 +0100 (BST) (envelope-from matthew@happy-idiot-talk.infracaninophile.co.uk) Received: (from matthew@localhost) by happy-idiot-talk.infracaninophile.co.uk (8.12.5/8.12.5/Submit) id g78ILVXD083563; Thu, 8 Aug 2002 19:21:31 +0100 (BST) Date: Thu, 8 Aug 2002 19:21:31 +0100 From: Matthew Seaman To: Cliff Cc: freebsd-questions@FreeBSD.ORG Subject: Re: Cron running scripts Message-ID: <20020808182131.GB82086@happy-idiot-talk.infracaninophi> References: <009201c23f5d$a48911b0$6401a8c0@tsunami> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <009201c23f5d$a48911b0$6401a8c0@tsunami> User-Agent: Mutt/1.5.1i Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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