在启动时将 python 脚本作为守护程序运行 (Ubuntu)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13718821/
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
Run python script as daemon at boot time (Ubuntu)
提问by and3p
I've created small web server using werkzeug and I'm able to run it in usual python way with python my_server.py. Pages load, everything works fine. Now I want to start it when my pc boots. What's the easiest way to do that? I've been struggling with upstart but it doesn't seem to "live in a background" cuz after I execute start my_serverI immediately receive kernel: [ 8799.793942] init: my_server main process (7274) terminated with status 1
我已经使用 werkzeug 创建了小型 Web 服务器,并且可以使用python my_server.py. 页面加载,一切正常。现在我想在我的电脑启动时启动它。最简单的方法是什么?我一直在与暴发户斗争,但它似乎并没有“生活在后台”因为我执行后start my_server我立即收到kernel: [ 8799.793942] init: my_server main process (7274) terminated with status 1
my_server.py:
my_server.py:
...
if __name__ == '__main__':
from werkzeug.serving import run_simple
app = create_app()
run_simple('0.0.0.0', 4000, app)
upstart configuration file my_server.conf:
新贵配置文件 my_server.conf:
description "My service"
author "Some Dude <[email protected]>"
start on runlevel [2345]
stop on runlevel [016]
exec /path/to/my_server.py
start on startup
Any Ideas how to make it work? Or any other better way to daemonize the script?
任何想法如何使它工作?或者任何其他更好的方法来守护脚本?
Update:I believe the problem lies within my_server.py. It doesn't seem to initiate the webserver (method run_simple()) in the first place. What steps should be taken to make .py file be run by task handler such as upstart?
更新:我相信问题出在my_server.py. 它似乎没有首先启动网络服务器(方法run_simple())。应采取哪些步骤使 .py 文件由任务处理程序(例如 upstart)运行?
- Place shebang as first line
#!/usr/bin/env python - Allow execution permissions
chmod 755 - Start the daemon with superuser rights (to be absolutely sure no permission restrictions prevents it from starting)
- Make sure all python libraries are there!
- Something else?
- 将shebang作为第一行
#!/usr/bin/env python - 允许执行权限
chmod 755 - 以超级用户权限启动守护进程(绝对确保没有权限限制阻止它启动)
- 确保所有 python 库都在那里!
- 还有什么?
Solved:The problem was with missing python dependencies. When starting the script through task manager (e.g. upstartor start-stop-daemon) no errors are thrown. Need to be absolutely sure that pythonpath contains everything you need.
已解决:问题在于缺少 python 依赖项。通过任务管理器(例如upstart或start-stop-daemon)启动脚本时,不会引发错误。需要绝对确定 pythonpath 包含您需要的一切。
回答by kaspersky
One simple way to do is using crontab:
一种简单的方法是使用 crontab:
$ crontab -e
A crontab file will appear for editing, write the line at the end:
将出现一个 crontab 文件进行编辑,在最后写一行:
@reboot python myserver.py
and quit. Now, after each reboot, the cron daemon will run your myserver python script.
并退出。现在,每次重新启动后,cron 守护程序将运行您的 myserver python 脚本。
回答by tvuillemin
In addition to gg.kaspersky method, you could also turn your script into a "service", so that you can start or stop it using:
除了 gg.kaspersky 方法,您还可以将脚本转换为“服务”,以便您可以使用以下方法启动或停止它:
$ sudo service myserver start
* Starting system myserver.py Daemon [ OK ]
$ sudo service myserver status
* /path/to/myserver.py is running
$ sudo service myserver stop
* Stopping system myserver.py Daemon [ OK ]
and define it as a startup service using:
并使用以下命令将其定义为启动服务:
$ sudo update-rc.d myserver defaults
To do this, you must create this file and save it in /etc/init.d/.
为此,您必须创建此文件并将其保存在 /etc/init.d/ 中。
#!/bin/sh -e
DAEMON="/path/to/myserver.py"
DAEMONUSER="myuser"
DAEMON_NAME="myserver.py"
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
d_start () {
log_daemon_msg "Starting system $DAEMON_NAME Daemon"
start-stop-daemon --background --name $DAEMON_NAME --start --user $DAEMONUSER --exec $DAEMON
log_end_msg $?
}
d_stop () {
log_daemon_msg "Stopping system $DAEMON_NAME Daemon"
start-stop-daemon --name $DAEMON_NAME --stop --retry 5 --name $DAEMON_NAME
log_end_msg $?
}
case "" in
start|stop)
d_
;;
restart|reload|force-reload)
d_stop
d_start
;;
force-stop)
d_stop
killall -q $DAEMON_NAME || true
sleep 2
killall -q -9 $DAEMON_NAME || true
;;
status)
status_of_proc "$DAEMON_NAME" "$DAEMON" "system-wide $DAEMON_NAME" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|force-stop|restart|reload|force-reload|status}"
exit 1
;;
esac
exit 0
In this example, I assume you have a shebang like #!/usr/bin/python at the head of your python file, so that you can execute it directly.
在这个例子中,我假设你在你的 python 文件的头部有一个像 #!/usr/bin/python 这样的shebang,这样你就可以直接执行它。
Last but not least, do not forget to give execution rights to your python server and to the service script :
最后但并非最不重要的一点是,不要忘记为您的 python 服务器和服务脚本授予执行权限:
$ sudo chmod 755 /etc/init.d/myserver
$ sudo chmod 755 /path/to/mserver.py
Here's the pagewhere I learned this originally (french).
这是我最初学到的页面(法语)。
Cheers.
干杯。
回答by tdihp
If you have supervisorservice that starts at boot, write a supervisor service is much, much simpler.
如果您有主管的服务,在开机启动时,写监事服务是非常非常简单。
You can even set autorestart if your program fails.
如果您的程序失败,您甚至可以设置自动重启。

