Dallaylaen has asked for the wisdom of the Perl Monks concerning the following question:
This is more of a Unix question rather than a Perl one, but still...
I'm looking for a way to stop my daemon, but I don't want to terminate an innocent bystander process. I came up with idea that PID file is created after the process has been spawned. Therefore, it's not older then the process, so the following code was written which seems to work correctly on my Ubuntu:
open (my $fd, "<", $conf->{pidfile}) or die "Failed to read pidfile $conf->{pidfile}: $!"; my $pid = <$fd>; chomp $pid; die "Broken pid file $conf->{pidfile}" unless $pid =~ /^\d+$/; # detect stale pid if ([stat $fd]->[9] >= ([stat "/proc/$pid"]->[9] || 9**9**9)) { print "Killing pid $pid...\n"; kill INT => $pid; };
Of course, it can still be tricked by touching the pid file, but then it's also possible to write rubbish into the pid file anyway.
Now I would like to ask what is the proper way of avoiding sending signal to a wrong process? CPAN has a multitude of modules for PID file handling, I was unable to choose one.
Thank you!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Detecting stale pid file under *nix
by andal (Hermit) on Oct 25, 2016 at 07:20 UTC | |
by Dallaylaen (Chaplain) on Oct 25, 2016 at 13:30 UTC | |
by andal (Hermit) on Oct 26, 2016 at 07:14 UTC | |
Re: Detecting stale pid file under *nix
by perlancar (Hermit) on Oct 25, 2016 at 07:26 UTC | |
by Dallaylaen (Chaplain) on Oct 25, 2016 at 08:19 UTC | |
Re: Detecting stale pid file under *nix
by Monk::Thomas (Friar) on Oct 25, 2016 at 09:02 UTC | |
by Dallaylaen (Chaplain) on Oct 25, 2016 at 13:05 UTC | |
Re: Detecting stale pid file under *nix
by afoken (Chancellor) on Oct 25, 2016 at 19:42 UTC |