>>> "Wakefield, Thad M." <twakefield at stcloudstate.edu> 03/09/05 8:10 AM
>>>
> Sent: Tuesday, March 08, 2005 8:16 PM
> 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?

I include this for kicks (because you're in perl, use perl to test it),
but I made a small C program some time ago to test this from shell
scripts:

-------------------------
// isbg.c

// isbg
// Troy Johnson
// 19990629

// A program for reporting if the current process is running in
//   the foreground, background, or without a controlling terminal.
//   1 == back, 0 == fore, -1 == no ctty

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

main()
{
        int fd;         // File descriptor.
        int p_stat = 0; // Process status flag.
        pid_t my_pgid;  // My process group id.
        pid_t f_pgid;   // Foreground process group id.

        if ((fd = open("/dev/tty", O_RDONLY)) >= 0)
        {
                my_pgid = getpgrp();
                f_pgid = tcgetpgrp(fd);
                close(fd);
                if (my_pgid == f_pgid)
                {       // If foreground process, set return value to 0.
                        p_stat = 0;
                }
                else
                {       // If background process, set return value to 1.
                        p_stat = 1;
                }
        }
        else
        {       // If no controlling tty, set return value to -1.
                p_stat = -1;
        }

        // Return value to user.
        printf("%d\n", p_stat);
        exit(0);
}
-------------------------

Have a good one,

Troy