in reply to Can a script tell if it is being run from inside a script versus in an interactive terminal session?

I know I can use -t/-p to find out about redirection, but it doesn't discriminate between a file redirected in/out versus a pipe to/from another command (which could be another source of error messages).

So my question I guess is more about parent/sibling process info-gathering. If I'm at a terminal running a script (whether or not it's piped), is there an easy way to determine whether the parent process is a terminal session versus a shell script? And if it's not a terminal session, is there a quick way to find out how many concurrent child processes are running?

My thinking is that if "I" (being the script that's running) have siblings, then I can assume that there's another command that is being piped from/to with whom my STDERR will mix, so I will need to prepend my STDERR output with the script name so the user knows where the error is coming from.

In the case where my parent is a shell script, whether or not I have any siblings, I'd still like to know where warnings/errors are coming from.

I know I can get the parent process ID with getppid(). Is there a perl way to get its number of children and is there a perl way to find out what the parent is?

  • Comment on Re: Can a script tell if it is being run from inside a script versus in an interactive terminal session?