On Tue, Mar 08, 2005 at 08:16:16PM -0600, Steve Swantz wrote:
> I'm sure this will be easy for someone, but I'm not finding it now...
> 
> I have a perl program that  normally runs by cron, but occasionally
> manually. I want to handle the output differently depending on how it
> is run. Yes, I could add a flag to the arguments, but I seem to
> remember there being a more fundamental way to determine at runtime.
> Anyone know?

Let me make your question more general...  "How to tell if a program
is running interactively".  I'm not aware of any simple way to test
specifically whether you're running under cron[1], but, if you're
running from cron, the program will be non-interactive and if you're
running manually from a shell, it will be interactive.

So:

sub running_interactively {
  return -t STDIN && -t STDOUT;
}

and then:

if (running_interactively()) {
  # behave in a console-appropriate manner
} else {
  # do stuff like you would when run from cron
}


[1]  I suppose you could recursively check your parent PID and use ps
or similar to look up that process to see if it's cron, but a) I
wasn't able to quickly find a way to determine PPID, b) even if I
could, it probably wouldn't work for ancestors beyond your direct
parent, and c) I doubt that it would be worth the trouble in any
case.

-- 
The freedoms that we enjoy presently are the most important victories of the
White Hats over the past several millennia, and it is vitally important that
we don't give them up now, only because we are frightened.
  - Eolake Stobblehouse (http://stobblehouse.com/text/battle.html)