6

So I use Upstart [myscript] to start a script [A], which starts another script [B], which in turn starts some other scripts [GROUP C].

When I run stop [myscript], I encounter the unusual behavior where [GROUP C] is also stopped. Every script in [GROUP C] has been started with a &, and are running in the background. I am surprised that they are also killed by the stop [myscript] command. Why is this, and how to I prevent it from happening?

EDIT: Basically [myscript] is as follows:

pre-start script
          echo 'Starting myscript Daemon\n'
end script

script
        exec /foo/bar/myscript-start.sh
end script

post-stop script
        echo 'and apparently we are done....'
end script
0

2 Answers 2

2

Upstart kills the process group:

The signal specified by the kill signal stanza is sent to the process group of the main process. (such that all processes belonging to the jobs main process are killed). By default this signal is SIGTERM.

To avoid this, start the child processes in a new session with setsid, for example:

setsid top

ps (ps -eo '%c %a %p %P %r') will show that the process has a unique process group, and a parent pid of 1.

1

Seems you don't understand how scripts stanzas works.

pre-start stanza

pre-start script
          echo 'Starting myscript Daemon\n'
end script

This script start before your main scrips, if it fails, the start sequence will fails. This is normally used for some adhoc operations that will allow the first script to start, these are not daemon-like (continuously running at background). Example:

pre-start script
    /lib/init/apparmor-profile-load usr.sbin.avahi-daemon
end script

script
    opts="-D"
    [ -e "/etc/eucalyptus/avahi-daemon.conf" ] && opts="${opts} -f /etc/eucalyptus/avahi-daemon.conf"
    exec avahi-daemon ${opts}
end script

As you can see, the apparmor profile is first load from the DBus (yeah, kind of awkward if you don't know the backend of Ubuntu) then starts the service as intended. This operation is designed for process that die once executed.

script stanza

script
        exec /foo/bar/myscript-start.sh
end script

This is the only part of your program that needs to run continuously and upstart has to track it's PID. This process dies when you stop the script.

post-stop stanza

post-stop script
        echo 'and apparently we are done....'
end script

This stanzas are designed as a kind of a cleanup process. It will be executed once the main script dies gracefully. For example, apport:

post-stop script
    # Check for a hung resume.  If we find one try and grab everything
    # we can to aid in its discovery
    if [ -e /var/lib/pm-utils/status ]
    then
        ps -wwef > /var/lib/pm-utils/resume-hang.log
    fi

    if [ "`dd if=/proc/sys/kernel/core_pattern count=1 bs=1 2>/dev/null`" != "|" ]
    then
        exit 1
    else
        echo 0 > /proc/sys/fs/suid_dumpable
        echo "core" > /proc/sys/kernel/core_pattern
    fi
end script

It tries to detect a resume from sleep that was not successful and starts preparations for detect the next hang so it can produce a useful report (yeah, is complicated again, but that was the most simple script I found apart ufw which is oversimplified). As you can see each process dies by itself since it could block the shutdown of the system (in such cases there's also a script that sends TERM and KILL signals to hanged process).

Bottom line, what you are experiencing is perfectly normal and is the desired behavior of upstart.

10
  • 1
    I think we have a bad case of miscommunication. I get what the stanzas do (for the most part, anyway) and their intended use. That isn't my problem. My problem is that when I say exec /foo/bar/myscript-start.sh, myscript-start.sh in turn starts a process which in turn starts multiple background processes. When I use upstart to stop myscript (stop myscript), all the background processes are also stopped. Nov 7, 2013 at 0:53
  • If I'm not mistaken, upstart kills the parent process and all it's child/forks. I'll look for the documentation.
    – Braiam
    Nov 7, 2013 at 1:00
  • 1
    Reading the cookbook, explaining the stop action, section 8 says "The signal specified by the kill signal stanza is sent to the process group of the main process. (such that all processes belonging to the jobs main process are killed). By default this signal is SIGTERM." You should edit your question with the content of the last comment so I can write that as answer.
    – Braiam
    Nov 7, 2013 at 1:21
  • 2
    This is valuable information and I would much appreciate it if you were to include it in your answer, but my actual question remains the same: "Why is this, and how to I prevent it from happening?" I figured upstart was killing the child group, I just don't know the reasoning or how to stop it from happening. Nov 7, 2013 at 1:28
  • 1
    Upstart and Systemd track which processes depend upon each other. Your upstart job requires myscript to be running for C to start. You must rewrite C's .init file to not depend upon myscript.
    – user535733
    Sep 11, 2014 at 16:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .