3

I've got a several services on Ubuntu which will start using 'upstart'. They are working as requested, but when I use 'stop/start/restart {myservice}' it will hang (but WILL do as requested).

I understand it has something to do with forking.

My services are python scripts, which will create new threads on startup. One script will create 1 new thread (and will continue running on the main as well), the second one will create 2 new threads and will continue running on the main as well and the third one will create no new threads.

All of them hang on the command.

All use the same code in /etc/init as follows:

description "my service"
version "1.0"
author "my name, 2013"

expect fork

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


chdir <to script dir>

exec /usr/bin/python ./scriptname/

what do you think might be the problem? Does 'fork' has anything to do with creating new threads?

JohnMighty
  • 73
  • 3
  • 8

2 Answers2

10

I think you are confusing creating threads in python with linux forking. Python will manage its own threads while it is running (assuming you are using regular constructs), from upstarts view it is only monitoring the running python process that you have started - python should be set up to manage its own children.

Possible solution 1

Try removing the expect clause altogether, then rebooting (you must reboot because upstart may be tracking a nonexistent PID).

I saw this start/stop hanging when upstart was tracking a nonexistent PID as a result of an incorrect expect, which was solved by removing the expect and rebooting. You can diagnose this by executing status myservice and upstart will report being stopped as well as a PID. (For other users whom rebooting is not an option, you could alternatively use this script)

Possible solution 2

Setting the user using exec su root -c "/usr/bin/python ./scriptname/" (or some other user) would solve the problem if python/your program is trying to access environment variables that only exist for a certian user (upstart has a very minimal set of environment variables by default)

Note

You should try see the output to help you debug. In real life you might pipe to logger however modifying your script to something like /usr/bin/python ./scriptname/ >> /home/myuser/output.log 2>&1 then viewing the contents would help you for now.

Community
  • 1
  • 1
Chris Riddell
  • 1,034
  • 7
  • 16
  • Another note, I don't think chdir is supported anymore in the latest version of upstart. You should also remove the stanza and use the full file path instead. – Chris Riddell Jan 22 '14 at 12:50
  • It's not necessary to reboot, and in fact it might be harmful if upstart hangs again upon trying, just follow the PID-rollover recipes laid out in http://askubuntu.com/questions/319199/upstart-tracking-wrong-pid-of-process-not-respawning – Josip Rodin Dec 18 '15 at 11:45
0

Yes indeed. See http://upstart.ubuntu.com/cookbook/#id160

Depending on wether your application forks or forks twice or not at all, you need expect fork, expect deamon or nothing, respectively.

durandal
  • 71
  • 1
  • 1