Linux 使用 virtualenv 在 debian 中守护 python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7807315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Daemonizing a python script in debian using virtualenv
提问by ingh.am
I've seen a lot of scripts for daemonizing a python script in linux, but not much information about how to use them. Could anyone guide me on this?
我已经看到很多用于在 linux 中守护 python 脚本的脚本,但没有太多关于如何使用它们的信息。有人可以指导我吗?
I currently have a lengthy python script that listens on a socket for an incoming message, if it's the correct format accepts it and then stores it into the database. The script itself just opens the socket and then listens on a while true (which does the job!) and does all the work in there.
我目前有一个冗长的 python 脚本,它在套接字上侦听传入消息,如果格式正确,则接受它,然后将其存储到数据库中。脚本本身只是打开套接字,然后监听一段时间为真(完成工作!)并在那里完成所有工作。
To daemonize it, would I have to modify my current script or call it from a separate script? I've seen examples of both but got neither to work.
要守护它,我是否必须修改我当前的脚本或从单独的脚本中调用它?我见过两者的例子,但都没有工作。
Also, I'm using virtualenv which might the root of my problems, any hints on using this with daemonized scripts?
另外,我正在使用 virtualenv 这可能是我问题的根源,有关将其与守护程序脚本一起使用的任何提示吗?
采纳答案by Some programmer dude
Create a shell-script that activates the virtual environment, and runs your Python script in the background.
创建一个 shell 脚本来激活虚拟环境,并在后台运行您的 Python 脚本。
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.
此外,虚拟环境中应该有一个 python 模块,您也可以从中导入和激活环境。我目前没有 virtualenv 工作,所以我无法检查它在哪里,但activate
在虚拟环境中搜索(或类似的东西),你应该找到它。
Edit: Added a minimal Debian init.d script
编辑:添加了一个最小的 Debian init.d 脚本
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.
要获得更完整的脚本,请复制/etc/init.d/skeleton
并编辑新文件。要编辑的重要部分是开头的块(程序使用的### BEGIN INIT INFO
和之间)以及、和变量。希望这就是制作启动脚本所需的全部内容。### END INIT INFO
update-rc.d
NAME
DAEMON
DAEMON_ARGS
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.
这<name of script>
只是名称,而不是完整路径。
回答by Jesse Smith
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.
当您需要在 python virtualenv 中运行应用程序时,您可以“激活”virtualenv,或使用该环境的唯一python
命令。
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
根据网站“如果您直接从 virtualenv 的 bin/ 目录(例如 path/to/env/bin/pip 或 /path/to/env/bin/python script.py)运行脚本或 python 解释器,则不需要用于激活。” - 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 below.)
我还有一些从源代码编译的 python 模块。那些需要在 PYTHONPATH 环境变量中。这可能是您的 virtualenv 激活的一部分,使用 virtualwrapper 完成,或显式调用(如下所示)。
Calling the program from an UPSTART job works as well. My example is above.
从 UPSTART 作业调用程序也可以。我的例子在上面。
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.
在 Amazon EC2 上的 Ubuntu 10.10 实例上,我使用 start-stop-daemon 命令运气更好。我还与其他一些新贵的“节”作斗争。我正在调用一个带有特定 virtualenv 的 python 应用程序和我执行的程序的一些参数。