http://www.perlmonks.org?node_id=897120

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/update_selena.pid') { die "Process is running" } open PID, '>/www/run/update_selena.pid' 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.

    fnord

      It's pretty straightforward. Variations that I tried are:
      system("update.pl &"); system("update.pl"); `update.pl &` `update.pl`
      And update.pl 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.

    fnord

      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.