Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: apache2 and system

by etcshadow (Priest)
on Sep 28, 2004 at 01:40 UTC ( #394431=note: print w/replies, xml ) Need Help??


in reply to apache2 and system

What you'll actually want to do is something more like:
my $pid = fork; die "Error forking: $!" unless defined $pid; if (!$pid) { my $pid = fork; die "Error forking: $!" unless defined $pid; if (!$pid) { exec("/path/to/foo"); } else { CORE::exit; } }

There are two important things in that:

  • You can't just say system("foo &"); to background foo. The problem is that a shell is invoked to execute "foo &", and, even though foo, itself, gets backgrounded (by that shell process), the shell process is waiting on the backgrounded job, and your perl process's call to system() is, in turn, waiting on the shell process... so you're really not accomplishing what you want (which is that foo can run entirely disconnected from your perl process).
  • I'm forking twice before launching foo. The reason for two forks is so that you can get a process that is entirely disconnected from the perl process. That is, the "foo" process won't appear as a child of the perl process, and won't send $SIG{CHLD}s back to the parent, or various other things that would otherwise be problematic (particularly in mod_perl). This double-fork with the middle-process exiting is a common idiom for completely disconnecting a child process (by making it not a child, but a grandchild, and killing the immediate child), and you can find mention of it in docs if you search for "child of init".

Anyway, hope that helps.

------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^2: apache2 and system
by sgifford (Prior) on Sep 28, 2004 at 02:59 UTC
    The shell launched by system doesn't seem to wait for the background process to complete, at least not in my tests. If the OS has an setsid shell command, is there any advantage of using fork and exec over just:
    system('setsid sleep 1000 &');
    It seems to work here.

    Also, the OP may want to think about what to do with open filehandles. It's possible the effects of having a second copy of STDOUT open by the child process will cause strange behavior (like never seeing EOF on a pipe/socket to that filehandle). Redirecting to /dev/null would solve that: system('setsid sleep 1000 </dev/null >/dev/null 2>/dev/null &');

      You're totally right, I spoke before I tested <smacks self>.

      It's easy enough to see if you just do something like:

      perl -e 'system("sleep 10")'
      which returns imediately. I must have been thinking about something else. (Particularly, I think I was thinking of some recent question in which someone wanted the exit status of the backgrounded process... and that's not at all the same issue... oh, well)
      ------------ :Wq Not an editor command: Wq
Re^2: apache2 and system
by water (Deacon) on Sep 28, 2004 at 01:46 UTC
    many thanks. could that helpful code snippet go onto a mason page inside apache2 w/o ill effect?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://394431]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2019-10-20 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?