Using ubuntu upstart to stop a daemon
Asked Answered
P

3

7

I am using upstart to create a daemon for a spawned nginx python fastcgi script. If I use the below it works:

sudo start myserver

What does not work is:

sudo stop myserver
stop: Unknown instance:

Below is my conf file with the command for stopping the process. I am assuming that the command for killing hte proccess is in pre-stop script?

#!upstart
description "myserver"
author      "Test"

start on startup
stop on shutdown
respawn
#instance

script
    export HOME="/root"

    echo $$ > /var/run/myerver.pid
    exec spawn-fcgi -d /home/ubuntu/workspace/rtbopsConfig/myserver/ -f /home/ubuntu/workspace/rtbopsConfig/myserver/index.py -a 127.0.0.1 -p 9001 >> /var/log/myserver.sys.log 2>&1
end script

pre-start script
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/myserver.sys.log
end script

pre-stop script
    rm /var/run/myserver.pid
    sudo kill `sudo lsof -t -i:9001`
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/myserver.sys.log
end script
Plumber answered 19/2, 2012 at 3:35 Comment(1)
*Nix questions get moved to superuser.comJohathan
D
8

The best way to handle this would be to tell spawn-fcgi to run in the foreground, and not daemonize. The man page for spawn-fcgi says that the -n option does this. Then you can rewrite this whole upstart job as this:

start on runlevel [2345]
stop on runlevel [016]
respawn    

exec spawn-fcgi -n -d /home/ubuntu/workspace/rtbopsConfig/myserver/ -f /home/ubuntu/workspace/rtbopsConfig/myserver/index.py -a 127.0.0.1 -p 9001 >> /var/log/myserver.sys.log 2>&1

Note that with Upstart 1.4 you don't even need the log direction, as it defaults to 'console log', which would have all the output in /var/log/upstart/$UPSTART_JOB.log.. so itw ould just be

start on runlevel [2345]
stop on runlevel [016]
respawn

exec spawn-fcgi -n -d /home/ubuntu/workspace/rtbopsConfig/myserver/ -f /home/ubuntu/workspace/rtbopsConfig/myserver/index.py -a 127.0.0.1 -p 9001

Its worth noting that this runs as root, but it listens on port 9001. So you would probably be better off running as nobody. With Upstart 1.5 (Ubuntu 12.04 and later again) Just add this:

setuid nobody
setgid nogroup

(You might have to change nobody/nogroup to ubuntu/ubuntu since your files are in /home/ubuntu)

Also note that start on startup will not work reliably, because the startup event happens before filesystems are mounted and before the network is up. Also start on shutdown just plain does not work, as it is not a real event. See man upstart-events for more events.

Deduction answered 7/9, 2012 at 19:13 Comment(2)
I think, you can make it work reliably if you change the start line a little to start on (filesystem and net-device-up IFACE=lo) or runlevel [2345]Fanniefannin
What if the process cannot be brought to the foreground? I have 2 commands, phpbrew fpm start and phpbrew fpm stop. I just want service start and service stop to translate to those 2 commands respectively. How can I do that?Rosemari
I
2

I believe your pre-stop is stopping your server, so when Upstart tries to stop your server there is no running process to stop and it gives you that error.

Incase answered 15/9, 2012 at 14:6 Comment(0)
N
0

If you daemonize your process you need to use the expect daemon or expect fork line so that upstart knows to track the new pid.

ex: start on startup stop on shutdown respawn expect daemon

Documentation can be found in upstart cookbook: http://upstart.ubuntu.com/cookbook/#expect-daemon http://upstart.ubuntu.com/cookbook/#expect-fork

Nakisha answered 16/12, 2015 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.