It seemed plausible that I might be able to check and see if the process had a parent, but when I run a process in the background its not really forked, at least from what I can tell.
Assuming UNIX/POSIX behaviour:
- all processes except init have a parent process id (though the parent process may have already exit()ed).
- conceptually at least, the only way to start a new process is to fork() an existing one. Which explains 1.
- you can test if your program is running interactively by seeing if STDIN and STDOUT are connected to terminals (see -t). Note that plain backgrounded programs can still be interactive, while fully daemonized programs aren't.
- IOW, running
interactively non-interactively isn't the same as running in the background. See also POSIX's tcgetpgrp() and tcsetpgrp().
| [reply] |
| [reply] |
| [reply] |
| [reply] |
| [reply] [d/l] |
Very nice...thank you it is what I needed.
This is running on HPUX and the one I found that works is the following:
$tpgrp = tcgetpgrp(fileno(*TTY));
$pgrp = getpgrp();
if ($tpgrp == $pgrp) {
system("which which > whichtest");
} else {
print "Skipping which as we are not interactive.\n";
}
I needed a change in this as the system call on HPUX gets the shell into a waiting state until it receives some input, which does not work well if we run it as a background process. Rather than skip doing this as a check, which we could do as the script is run manually at times, I wanted a way to have it run only when we needed it.
| [reply] [d/l] |