Skip to content

Instantly share code, notes, and snippets.

@jettero
Created July 12, 2017 13:56
Show Gist options
  • Save jettero/5076ae465c4a3d0d18936fe337e656f4 to your computer and use it in GitHub Desktop.
Save jettero/5076ae465c4a3d0d18936fe337e656f4 to your computer and use it in GitHub Desktop.
work around a goofy problem in upstart
#!/usr/bin/env perl
# occasionally when you're designing upstart services in /etc/init/whatever.conf
# you pick the wrong 'no expect' `expect fork` or `expect daemon` and upstart beings
# tracking a pid that is already dead. It'll be stuck there refusing to start or stop the service until
# it tracks the pid exiting again. So this script forkexists over and over and over until it gets the pid
# upstart is waiting for, then exits.
# NOTE: sometimes the upstart user prevents this from working, if applicable
# ssh to localhost to make sure our ppid is 1 after we die.
# The script attempts to detect this scenerio and exists with either:
# "really might work" or
# "need to use ssh trick, see notes in file"
#
# ssh localhost workaround-upstart-snafu $stupidpid
use strict;
use POSIX qw(setsid);
my $target = shift;
die "pid?" unless $target and int $target;
die "... er, that's running" if -d "/proc/$target";
my $ppid = $$;
$SIG{USR1} = sub { print "we did it!! \\o/\n"; exit 0 };
while( my $pid = fork ) {
waitpid($pid, 0);
}
print "hai, I'm $$\n";
if( $$ == $target ) {
setsid();
kill 10, $ppid;
sleep 2;
my $_ppid = getppid();
print "\n\nexiting... old ppid=$ppid, new ppid=$_ppid", ($_ppid == 1 ? " (might really work)" : " (need to use ssh trick, see notes in file)"), "\n\n";
exit 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment