繁体   English   中英

在* nix系统中的python中自动重启守护进程

[英]self restarting daemon in python in *nix system

我需要在python中创建一个守护进程。 我做了搜索,发现了一段很好的代码 应该在系统启动后自动启动守护程序,如果意外关闭它应该启动它。 在Unix环境中经历了关于高级编程中守护进程的章节,并且有两个问题。

要在启动后自动运行脚本,我需要将我的守护程序脚本放到/etc/init.d。 那是对的吗?

我该怎么办才能重生这个守护进程? 根据本书,我需要在/ etc / inittab中添加一个respawn条目,但我的系统上没有/ etc / inittab。 我应该自己创造吗?

如果你在Ubuntu上,我建议你看看暴发户 它比inittab更好,但确实涉及一些学习曲线。

编辑 (布莱尔):这是我最近为自己的一个程序编写的新手脚本的改编示例。 像这样的基本新贵脚本是相当可读/可理解的(虽然很多这样的东西),当你开始做一些奇特的东西时,它们会变得复杂。

description "mydaemon - my cool daemon"

# Start and stop conditions. Runlevels 2-5 are the 
# multi-user (i.e, networked) levels. This means 
# start the daemon when the system is booted into 
# one of these runlevels and stop when it is moved
# out of them (e.g., when shut down).
start on runlevel [2345]
stop on runlevel [!2345]

# Allow the service to respawn automatically, but if
# crashes happen too often (10 times in 5 seconds) 
# theres a real problem and we should stop trying.
respawn
respawn limit 10 5

# The program is going to daemonise (double-fork), and
# upstart needs to know this so it can track the change
# in PID.
expect daemon

# Set the mode the process should create files in.
umask 022

# Make sure the log folder exists.
pre-start script
    mkdir -p -m0755 /var/log/mydaemon
end script

# Command to run it.
exec /usr/bin/python /path/to/mydaemon.py --logfile /var/log/mydaemon/mydaemon.log

要创建守护程序,请使用双叉(),如您找到的代码中所示。 然后,您需要为守护程序编写一个init脚本并将其复制到/etc/init.d/中。

http://www.novell.com/coolsolutions/feature/15380.html

有许多方法可以指定守护程序的自动启动方式,例如chkconfig。

http://linuxcommand.org/man_pages/chkconfig8.html

或者,您可以手动为某些运行级别创建符号链接。

最后,您需要在意外退出时重新启动服务。 您可以在/ etc / inittab中包含serivce的respawn条目。

http://linux.about.com/od/commands/l/blcmdl5_inittab.htm

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM