我建議你看看upstart如果你在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
我在示例腳本新貴編輯從我的項目之一,希望你不要介意。 – Blair 2012-08-13 21:40:42
@Blair感謝您的有用編輯/添加。 – 2012-08-14 08:05:39
如果你也使用[python-daemon](https://pypi.python.org/pypi/python-daemon/),那麼請注意'expect守護進程'節最好避免 - python-daemon將會出現你正在使用暴發戶,並不會雙叉。 – 2013-06-19 13:21:47