Re: Detecting running from cron?
by chipmunk (Parson) on Jan 23, 2001 at 20:54 UTC
|
I think the simplest way to tell whether you're running from cron is to set an environment variable in the crontab, and then check for the existence of that variable in the script.
crontab:
CRON = 1
15 * * * * myscript.pl
script:
#!/usr/local/bin/perl
if ($ENV{CRON}) {
print STDERR "Running in cron.\n";
} else {
print "Not running in cron.\n";
}
Of course, that depends on the variable being set in the crontab file.
| [reply] [d/l] [select] |
Re: Detecting running from cron?
by tadman (Prior) on Jan 23, 2001 at 20:48 UTC
|
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. | [reply] [d/l] [select] |
|
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.
| [reply] [d/l] |
|
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.
| [reply] |
(tye)Re: Detecting running from cron?
by tye (Sage) on Jan 23, 2001 at 20:57 UTC
|
I think the canonical test is -t which is
the same as -t STDIN. For your case
-t STDOUT might be better.
-
tye
(but my friends call me "Tye")
| [reply] [d/l] [select] |
|
-t STDIN is definitely very useful; it tells you whether the script is running interactively. But keep in mind that a script can be run from the command line without being run interactively:
echo "Hi!" | perl -wle 'print -t() ? "" : "not ", "interactive"'
So it might not do what the original poster wants in this case.
| [reply] [d/l] [select] |
|
Thank you. I was relying on the readers to consult the
manual on -t (especially since I didn't say whether a true
or false value from -t meant "running from cron"). But
that was a pretty poor assumption on my part.
Reading between the lines of the question, I got the
impression that the stuff going to STDOUT was meant only
to be seen by interactive users, which is why I suggested
-t STDOUT. Thinking on it more, I think a
better test for this case is -t STDERR so that
script <input | more would be considered
"interactive" while the cron case specifically stated that
STDERR was redirected to a file.
-
tye
(but my friends call me "Tye")
| [reply] [d/l] [select] |
Re (tilly) 1: Detecting running from cron?
by tilly (Archbishop) on Jan 23, 2001 at 21:46 UTC
|
If your cron is properly configured it will automatically
send email if any output went to STDOUT or STDERR. So I
would recommend making sure your .forward file is what
you want and don't worry about it.
But I do suggest that you take a look at Get default login environment - it
may protect you from a common gotcha. | [reply] |
Re: Detecting running from cron?
by mr.nick (Chaplain) on Jan 23, 2001 at 20:49 UTC
|
Hm, good question: how for a script to determine how it was invoked: command line vs. crontab.
The only thing I can offer is that when run from crond, different environment variables are set. The one that seems to be helpful is $TERM. When a script is run from crond, at least on my Redhat 6.x system, TERM is set to "dumb".
So maybe something like:
if ($ENV{TERM} eq 'dumb') {
## redirect stdout to stderr
open STDOUT,">&STDERR"; ## I think :)
}
print "This will go to stderr if running from crond\n";
| [reply] [d/l] |
Re: Detecting running from cron?
by AgentM (Curate) on Jan 24, 2001 at 00:05 UTC
|
You have some ad hoc environment var methods above which I would avoid. The "official" and far more portable way is using tilly's method with -t which is equivalent to the POSIX::isatty(...).
AgentM Systems nor Nasca Enterprises nor
Bone::Easy nor Macperl is responsible for the
comments made by
AgentM. Remember, you can build any logical system with NOR.
| [reply] |
|
Official, portable, and solves a different problem. -t tells you whether a filehandle is connected to a TTY; it does not tell you whether you are running from the command line.
Any filehandle you can test with -t can be redirected on the command line. Using -t to ask "is the script running from the command line?" will result in false negatives.
P.S. Did you mean tye's method?
| [reply] [d/l] |