Help with respawn

Alan Taylor alanupstart at gmail.com
Wed Apr 18 21:21:40 BST 2007


I'm trying to understand how the respawn keyword works in a job file.  See
the attached files for details.

I create a simple Perl daemon (attached "tmp-simple-pl.txt") that dummy
loops.  My Upstart job definition file is the attached "
etc-event-d-simple.txt".

When I execute "initctl emit simpleup" I end up getting 10 instances of "
simple.pl" (see attached "ps-ef-grep-simple.txt").  If I take away the
"respawn" keyword in the job definition file then I get a single instance of
"simple.pl"

I can't figure out why I get 10 instances instead of just the single
instance.  Does it have something to do with the fact that the "simple job"
runs until stop since the simple.pl daemon forks off from init?  Can Upstart
supervise this daemon adequately?

Thanks,
Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.ubuntu.com/archives/upstart-devel/attachments/20070418/9ef3e2dc/attachment.htm 
-------------- next part --------------
localhost:/ # cat /etc/event.d/simple

start on simpleup
stop on simpledown

respawn

script
    exec /tmp/simple.pl
end script
-------------- next part --------------
localhost:/ # initctl emit simpleup
simpleup
simple (start) waiting
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12933
simple (start) post-start, (main) process 12933
simple (start) running, process 12933
-------------- next part --------------
localhost:/ # initctl events
simpleup
starting simple
started simple
stopping simple ok
starting simple
started simple
stopping simple ok
starting simple
started simple
stopping simple ok
starting simple
started simple
stopping simple ok
starting simple
started simple
stopping simple ok
starting simple
started simple
stopping simple ok
starting simple
started simple
stopping simple ok
starting simple
started simple
stopping simple ok
starting simple
started simple
stopping simple ok
starting simple
started simple
stopping simple ok
stopped simple failed respawn
    EXIT_STATUS=0
-------------- next part --------------
localhost:~ # initctl jobs
simple (start) waiting
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12933
simple (start) post-start, (main) process 12933
simple (start) running, process 12933
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12935
simple (start) post-start, (main) process 12935
simple (start) running, process 12935
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12937
simple (start) post-start, (main) process 12937
simple (start) running, process 12937
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12939
simple (start) post-start, (main) process 12939
simple (start) running, process 12939
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12941
simple (start) post-start, (main) process 12941
simple (start) running, process 12941
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12943
simple (start) post-start, (main) process 12943
simple (start) running, process 12943
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12945
simple (start) post-start, (main) process 12945
simple (start) running, process 12945
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12947
simple (start) post-start, (main) process 12947
simple (start) running, process 12947
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12949
simple (start) post-start, (main) process 12949
simple (start) running, process 12949
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (start) starting
simple (start) pre-start
simple (start) spawned, process 12951
simple (start) post-start, (main) process 12951
simple (start) running, process 12951
simple (start) stopping
simple (start) killed
simple (start) post-stop
simple (stop) starting
simple (stop) starting
simple (stop) waiting
-------------- next part --------------
localhost:/etc/event.d # !p
ps -ef | grep simple
root     12934     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl
root     12936     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl
root     12938     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl
root     12940     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl
root     12942     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl
root     12944     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl
root     12946     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl
root     12948     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl
root     12950     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl
root     12952     1  0 14:54 ?        00:00:00 /usr/bin/perl -w /tmp/simple.pl

-------------- next part --------------
localhost:/ # cat /tmp/simple.pl
#!/usr/bin/perl -w
#
# Skeleton from http://www.perlmonks.org/?node_id=121604
#
use strict;
use POSIX qw( setsid );

my $debug = 1;
my $logfile = q(.testlog);

# Season to taste
my @fh_unused = (\*STDIN, \*STDOUT);

open \*STDERR, ">> $ENV{'HOME'}/$logfile";
select((select(\*STDERR), $| = 1)[0]);

{
# Daemon Rule 1) Fork and exit the parent.
    my $ppid = $$;
    my $pid = fork and exit 0;
    ! defined $pid and die "No Fork: ", $!;
    while (kill 0, $ppid) {
        select undef, undef, undef, .001;
    };
}

# Daemon Rule 2) become session leader, pg leader, no term
my $session_id = POSIX::setsid();

# Daemon Rule 3) cd to /
chdir '/' or die "Could not cd to rootfs", $!;

# Daemon Rule 4) set file creation mask to 0
my $oldmask = umask 00;

# Daemon Rule 5) Close unneeded file handles
close $_ or die $! for @fh_unused;

while ( 1 )
{
    sleep ( 5 );
}

exit 0;


More information about the upstart-devel mailing list