The Python Oracle

Daemonizing a python script in debian using virtualenv

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 5 Looping

--

Chapters
00:00 Daemonizing A Python Script In Debian Using Virtualenv
00:44 Accepted Answer Score 7
02:05 Answer 2 Score 6
03:15 Thank you

--

Full question
https://stackoverflow.com/questions/7807...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #linux #debian #daemon #virtualenv

#avk47



ACCEPTED ANSWER

Score 7


Create a shell-script that activates the virtual environment, and runs your Python script in the background.

Also, there should by a python module in the virtual environment that you can import and activate the environment from too. I don't have virtualenv working at the moment, so I can not check where it is, but search for activate (or something similar) in the virtual environment and you should find it.

Edit: Added a minimal Debian init.d script

The absolute minimal script needed to start a daemon when the computer boots, is this:

#!/bin/sh
/path/to/program &

The & makes the program run in the background, so it wont stop the rest of the boot process.

For a more complete script, copy /etc/init.d/skeleton and edit the new file. The important part to edit is the block at the beginning (between ### BEGIN INIT INFO and ### END INIT INFO, which is used by the update-rc.d program), and the NAME, DAEMON and DAEMON_ARGS variables. Hopefully that should be all that's needed for making a startup-script.

Activate the script as this:

sudo update-rc.d <name of script> defaults
sudo update-rc.d <name of script> enable

And to start it:

sudo update-rc.d <name of script> start

The <name of script> is just the name, not the full path.




ANSWER 2

Score 6


script
  export PYTHONPATH=.:/home/ubuntu/.local/lib/python2.7/site-packages/:/home/ubuntu/python/lib/python2.7/site-packages/
  exec start-stop-daemon --start  --chuid ubuntu --exec /home/ubuntu/python_envs/MyProj/bin/python /home/ubuntu/www/MyProj/MyProj.py -- --config-file-dir=/home/ubuntu/www/MyProj/config/ >> /home/ubuntu/startup.log 2>&1 &
end script

When you need to run an application in a python virtualenv, you can either 'activate' the virtualenv, or use that environment's unique python command.

As per the website "If you directly run a script or the python interpreter from the virtualenv's bin/ directory (e.g. path/to/env/bin/pip or /path/to/env/bin/python script.py) there's no need for activation." - http://pypi.python.org/pypi/virtualenv

I also have some python modules that were compiled from source. Those need to be in the PYTHONPATH environment variable. That could be part of your virtualenv activation, done with virtualwrapper, or explicitly called (as I do in my example above).

Calling the program from an UPSTART job works as well. My example is above.

On an Ubuntu 10.10 instance on Amazon EC2, I had better luck with the start-stop-daemon command. I also struggled with some of the other upstart 'stanzas.' I am calling a python application with a specific virtualenv and some parameters to my executed program.