使用 virtualenv 在 debian 中守护 python 脚本
我见过很多在 Linux 中守护 python 脚本的脚本,但关于如何使用它们的信息不多。有人可以指导我吗?
我目前有一个冗长的 python 脚本,它在套接字上侦听传入消息,如果格式正确,则接受它,然后将其存储到数据库中。脚本本身只是打开套接字,然后监听一段时间(这完成了工作!)并完成其中的所有工作。
要对其进行守护进程,我是否必须修改当前脚本或从单独的脚本调用它?我见过这两种情况的例子,但都不起作用。
另外,我正在使用 virtualenv,这可能是我问题的根源,关于将其与守护进程脚本一起使用的任何提示吗?
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?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个激活虚拟环境的 shell 脚本,并在后台运行 Python 脚本。
另外,虚拟环境中应该有一个 python 模块,您也可以从中导入和激活环境。我目前没有 virtualenv 工作,所以我无法检查它在哪里,但在虚拟环境中搜索
activate
(或类似的东西),你应该找到它。编辑:添加了一个最小的 Debian init.d 脚本
在计算机启动时启动守护进程所需的绝对最小脚本是这样的:
&
使程序在背景,因此它不会停止其余的启动过程。要获得更完整的脚本,请复制
/etc/init.d/sculpture
并编辑新文件。要编辑的重要部分是开头的块(在### BEGIN INIT INFO
和### END INIT INFO
之间,由update 使用-rc.d
程序),以及NAME
、DAEMON
和DAEMON_ARGS
变量。希望这就是制作启动脚本所需的全部内容。按如下方式激活脚本:
启动脚本:
只是名称,而不是完整路径。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:
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 theupdate-rc.d
program), and theNAME
,DAEMON
andDAEMON_ARGS
variables. Hopefully that should be all that's needed for making a startup-script.Activate the script as this:
And to start it:
The
<name of script>
is just the name, not the full path.当您需要在 python virtualenv 中运行应用程序时,您可以“激活”virtualenv,或使用该环境的唯一
python
命令。根据网站“如果您直接从 virtualenv 的 bin/ 目录运行脚本或 python 解释器(例如 path/to/env/bin/pip 或 /path/to/env/bin/python script.py),则不需要用于激活。” - http://pypi.python.org/pypi/virtualenv
我还有一些 python 模块从源代码编译的。这些需要位于 PYTHONPATH 环境变量中。这可能是 virtualenv 激活的一部分,使用 virtualwrapper 完成,或者显式调用(正如我在上面的示例中所做的那样)。
从 UPSTART 作业调用程序也可以。我的例子就是上面的。
在 Amazon EC2 上的 Ubuntu 10.10 实例上,我使用 start-stop-daemon 命令的运气更好。
我还为其他一些新贵的“节”而苦苦挣扎。我正在使用特定的 virtualenv 和我执行的程序的一些参数来调用 python 应用程序。
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.