Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ubuntu upstart to stop a daemon

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
like image 259
Tampa Avatar asked Feb 19 '12 03:02

Tampa


2 Answers

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.

like image 161
SpamapS Avatar answered Oct 04 '22 23:10

SpamapS


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.

like image 23
maqifrnswa Avatar answered Oct 04 '22 23:10

maqifrnswa