Jim Trocki on Wed, 8 Jan 2003 12:59:56 -0500


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Detecting Background


On Wed, 8 Jan 2003, Urb LeJeune wrote:

> 	Is it possible for a Perl script to detect if it's running as a
> background task? If running from the command line I want to
> display progress messages, if running as a cron job I don't want
> to print. I could pass a parameter when running as a cron job but
> wanted to see if there was an easier way.

unix doesn't really have a clear distinction about what a "background
task" is (that's more of a distinction made by the shell), but you could
check to see if stdout and stdin are not attached to a tty:

if (! -t STDOUT && ! -t STDIN)
{
    # check us out, we're possibly a "background task"
}

perhaps you could get tricky and have the process check to see if cron
is the ppid, but that's kinda messy.

the safest bet would be to pass it an argument telling it what
the behavior should be, and make the default behavior be the most
commonly-expected behavior.

-
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**