Skip Menu |

This queue is for tickets about the App-Daemon CPAN distribution.

Report information
The Basics
Id: 44516
Status: open
Priority: 0/
Queue: App-Daemon

People
Owner: Nobody in particular
Requestors: felix.ostmann [...] thewar.de
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.06
Fixed in: (no value)



Subject: Set $SIG{INT} and $SIG{TERM} so myapp.pid get deleted after ./myapp_script.pl stop
Perhaps i miss a thing, but after ./MyApp.pl stop the pidfile will not deleted?!?! So i installed INT and TERM ... i think it could be a good idea to check the ref of the old $SIG and then make a callback, calling the old coderef, after deleting the pidfile? my $old_sigdie = $SIG{__DIE__}; $SIG{__DIE__} = sub { # after your work $SIG{__DIE__} = $old_sigdie; die(@_); }; i dont know, is this possible? What is with a local $SIG{__DIE__} only while starting daemonize? :-/ so this will make the local keeping alive i think. So this is not as good as i think befor.
Subject: Daemon.pm.patch
--- Daemon.pm.orig 2009-03-24 12:09:39.000000000 +0100 +++ Daemon.pm 2009-03-24 13:06:37.000000000 +0100 @@ -144,6 +146,10 @@ } }; + $SIG{INT} = $SIG{TERM} = sub { + unlink $pidfile or warn "Cannot remove $pidfile"; + }; + INFO "Process ID is $$"; pid_file_write($$); INFO "Written to $pidfile";
From: felix.ostmann [...] thewar.de
I donk like to overwrite $SIG{__DIE__} ... so perhaps a "END { }" or "use Guard" is a better way ... But we need to set INT and TERM i think. Perhaps the parent should send a signal to every child "{ local $SIG{INT} = 'IGNORE'; kill 'INT' => -$$;}" while stopping. On Di. 24. Mär. 2009, 08:16:56, Sadrak wrote: Show quoted text
> Perhaps i miss a thing, but after ./MyApp.pl stop the pidfile will not > deleted?!?! > > So i installed INT and TERM ... > > i think it could be a good idea to check the ref of the old $SIG and > then make a callback, calling the old coderef, after deleting the pidfile? > > my $old_sigdie = $SIG{__DIE__}; > $SIG{__DIE__} = sub { > # after your work > $SIG{__DIE__} = $old_sigdie; > die(@_); > }; > > i dont know, is this possible? What is with a local $SIG{__DIE__} only > while starting daemonize? :-/ so this will make the local keeping alive > i think. So this is not as good as i think befor.
Another patch, now using END {} and $master_pid, that pretend childs for executing the END. Dont know if this is the right way :-/ i will now use this and wait for some replies. On Di. 24. Mär. 2009, 09:08:08, Sadrak wrote: Show quoted text
> I donk like to overwrite $SIG{__DIE__} ... so perhaps a "END { }" or > "use Guard" is a better way ... > > But we need to set INT and TERM i think. Perhaps the parent should send > a signal to every child "{ local $SIG{INT} = 'IGNORE'; kill 'INT' => > -$$;}" while stopping. > > On Di. 24. Mär. 2009, 08:16:56, Sadrak wrote:
> > Perhaps i miss a thing, but after ./MyApp.pl stop the pidfile will not > > deleted?!?! > > > > So i installed INT and TERM ... > > > > i think it could be a good idea to check the ref of the old $SIG and > > then make a callback, calling the old coderef, after deleting the
pidfile? Show quoted text
> > > > my $old_sigdie = $SIG{__DIE__}; > > $SIG{__DIE__} = sub { > > # after your work > > $SIG{__DIE__} = $old_sigdie; > > die(@_); > > }; > > > > i dont know, is this possible? What is with a local $SIG{__DIE__} only > > while starting daemonize? :-/ so this will make the local keeping alive > > i think. So this is not as good as i think befor.
>
--- Daemon.pm.orig 2009-03-24 12:09:39.000000000 +0100 +++ Daemon.pm 2009-03-24 14:22:12.000000000 +0100 @@ -21,6 +21,7 @@ $loglevel, $action, $appname); $action = ""; $appname = appname(); +my $master_pid; ########################################### sub cmd_line_parse { @@ -136,22 +139,30 @@ if( $background ) { detach( $as_user ); } - - $SIG{__DIE__} = sub { - # Make sure it's not an eval{} triggering the handler. - if(defined $^S && $^S==0) { - unlink $pidfile or warn "Cannot remove $pidfile"; - } - }; - INFO "Process ID is $$"; - pid_file_write($$); + $SIG{INT} = sub { exit; }; + $SIG{TERM} = sub { exit; }; + + $master_pid = $$; + INFO "Process ID is $master_pid"; + pid_file_write($master_pid); INFO "Written to $pidfile"; return 1; } ########################################### +sub END { +########################################### + if( $master_pid && $master_pid == $$ ) { + INFO "Stopping Process with ID $master_pid"; + local $SIG{INT} = 'IGNORE'; + kill 'INT' => -$master_pid; + unlink $pidfile or WARN "Cannot remove $pidfile"; + } +} + +########################################### sub detach { ########################################### my($as_user) = @_;
Not sure I'm following here, guys, two thoughts: 1) As it's implemented right now, the pid file sticks around even after the process is gone. 2) What kind of problems do you want to solve with the DIE/END handlers? Maybe a practical example would help.
On Di. 24. Mär. 2009, 12:02:56, MSCHILLI wrote: Show quoted text
> Not sure I'm following here, guys, two thoughts: > > 1) As it's implemented right now, the pid file sticks around even after > the process is gone.
the pid file is still alive, only the daemon die ($SIG{__DIE__} called), the pidfile get removed. i think the pidfile must be removed when the daemon stop. Show quoted text
> > 2) What kind of problems do you want to solve with the DIE/END handlers? > Maybe a practical example would help.
you send a kill 2 => $pid to the daemon when you execute myapp_script.pl stop but 'INT' let the daemon terminate and not called the $SIG{__DIE__}, so the pidfile is still alive. 'TERM' is for a "pkill myapp" in bash (sending kill 15). Simple example: # perl -MApp::Daemon -e 'App::Daemon::daemonize; sleep 1000;' # perl -MApp::Daemon -e 'App::Daemon::daemonize; sleep 1000;' stop # cat /tmp/-e.pid 12345