Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Is Process Running In The Background?

by gokuraku (Monk)
on Apr 28, 2008 at 11:39 UTC ( [id://683275]=perlquestion: print w/replies, xml ) Need Help??

gokuraku has asked for the wisdom of the Perl Monks concerning the following question:

I'm kind of looking for some brainstorming here, my problem is this. I have a perl script that runs some various commands as part of a test harness, one of them gets hung but only when the perl script is run as a background process. I would like to set my script so that if its running as a background process then skip that test, otherwise run it, but I am unsure what is the best way to tell if the script is being run in the background.
In looking at Proc::Background I thought it might be able to do it by that, but I didn't see an obvious way to check if the process itself was being run in the background. 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.
Has anyone tried something similar to this before? I'd like to get some ideas and see what I can figure out, its a good mental exercise.
Thanks!

Replies are listed 'Best First'.
Re: Is Process Running In The Background?
by Joost (Canon) on Apr 28, 2008 at 13:21 UTC
    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:
    1. all processes except init have a parent process id (though the parent process may have already exit()ed).
    2. conceptually at least, the only way to start a new process is to fork() an existing one. Which explains 1.
    3. 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.
    4. IOW, running interactively non-interactively isn't the same as running in the background. See also POSIX's tcgetpgrp() and tcsetpgrp().
Re: Is Process Running In The Background?
by hipowls (Curate) on Apr 28, 2008 at 12:12 UTC

    There is IO::Interactive that you can use to test if your program is running interactively.

Re: Is Process Running In The Background?
by haoess (Curate) on Apr 28, 2008 at 12:17 UTC

    A quick search revealed this thread on Google Groups.

    -- Frank

Re: Is Process Running In The Background?
by rowdog (Curate) on Apr 28, 2008 at 19:14 UTC
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://683275]
Approved by Corion
Front-paged by sgifford
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-23 22:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found