Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

fork and spoon

by Sly_G (Novice)
on Apr 02, 2011 at 21:37 UTC ( #897120=perlquestion: print w/replies, xml ) Need Help??
Sly_G has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I made updater-script that daemonizes itself like this:
sub Daemonize { return if ($^O eq 'MSWin32'); chdir '/' or die "Can't chdir to /: $!"; umask 0; open STDIN, '/dev/null' or die "Can't read /dev/null: $!"; open STDOUT, '>/dev/null' or die "Can't write to '>/dev/null': $!"; open STDERR, '>/dev/null' or die "Can't write to /dev/null: $!"; defined(my $pid = fork) or die "Can't fork: $!"; exit if $pid; setsid or die "Can't start a new session: $!"; if (-e '/www/run/') { die "Process is running" } open PID, '>/www/run/' or die "Can't write pid: $!" +; print PID $$; close PID; }

It does it ok, if I run it from command line.

Then I made a script-manager for several updater-scripts like this (all of them are supposed to run independently for long periods of time). Manager should be able to run them, to show us if particular updater is running or not, and to kill running updaters (using pid files they're saving for themselves).

So, when I try to run such an updater from a manager, the latter hangs and waits until updater is done. Which is not what I inteded. No matter if I use backticks or system().

What is it that I'm doing wrong? Please, help.

Replies are listed 'Best First'.
Re: fork and spoon
by Illuminatus (Curate) on Apr 03, 2011 at 00:44 UTC
    The first thing you are doing wrong is not posting the code (ie the manager, or at least the part that is running the updater) that is not working :). Without seeing what you are doing in the code that is not working, it is very hard to diagnose. I can't tell what you are actually calling in the backticks or system(), whether it is hanging where you think it is hanging, etc.


      It's pretty straightforward. Variations that I tried are:
      system(" &"); system(""); ` &` ``
      And is -rwxr-xr-x and it starts OK from bash shell.
Re: fork and spoon
by Illuminatus (Curate) on Apr 04, 2011 at 11:57 UTC
    Are you sure it is hanging on the actual system/backtick line that invokes the updater? You listed several functions that the manager can/will do. Does it always do all of them? It would seem like depending on how it was invoked, it would either start, show status, or kill. Is that the case? Are the updaters called within a loop? If so, I think you should post the loop.


      Well, thanks for concern - the problem has been solved. The child after fork must call setsid - after that he is officially cutted from parent and Apache closes connection.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://897120]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2018-07-22 07:27 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (452 votes). Check out past polls.