in reply to Detecting running from cron?

Define a variable in your crontab, which can be detected by the script:
CRON = 1 * * * * * scriptname
Check for it in the script and direct output accordingly:
BEGIN { open (STDOUT, ">&STDERR") if ($ENV{'CRON'}); }
This will allow you to print normally, but it will be directed to STDERR using a redirect. The Camel Book has more detail on the "&" operator in the context of an open request, as does "perldoc -f open".

Note: chipmunk's reply below had a better (i.e. working) solution which I have incorporated here to avoid giving out bad (i.e. broken) advice.

Previously I suggested checking for the environment variable '_', which seemed to only appear in cron, but is in fact, an artifact of the 'sh' shell and derivatives, such as bash. I used 'tcsh' for interactive, and it does not have that variable defined, hence the confusion.

Replies are listed 'Best First'.
Re: Re: Detecting running from cron?
by turnstep (Parson) on Jan 23, 2001 at 21:27 UTC
    Not good, because that environment variable also (ususally) exists from the command line. Try this:
    perl -e 'print "foo\n" if exists $ENV{_};'
    Best to set a variable in the crontab file, as mentioned below.
      It would seem that the '_' variable is a bash/sh thing, and as I use 'tcsh' for most of my work normally I didn't notice that. Good catch.

      chipmunk has the best approach, with setting an environment variable within your crontab.