Ubuntuを使用している場合は、upstartを調べることをお勧めします。正直に言うと、それよりもはるかに優れてinittab
いますが、ある程度の学習曲線が必要です。
編集(ブレアによる):これは私が最近自分のプログラムの1つのために書いたアップスタートスクリプトの適応例です。このような基本的なupstartスクリプトは、かなり読みやすく、理解しやすいものですが、(多くのそのようなものと同様に)凝ったことを始めたときに複雑になる可能性があります。
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