3

Pythonでデーモンを作成する必要があります。私は検索を行い、良いコードを見つけました。デーモンは、システムの起動後に自動的に起動する必要があり、予期せず閉じられた場合は起動する必要があります。Unix環境での高度なプログラミングのデーモンに関する章を読みましたが、2つの質問があります。

起動後にスクリプトを自動的に実行するには、デーモンスクリプトを/etc/init.dに配置する必要があります。あれは正しいですか?

デーモンをリスポーンするにはどうすればよいですか?この本によると、リスポーンエントリを/ etc / inittabに追加する必要がありますが、システムに/ etc/inittabがありません。自分で作成する必要がありますか?

4

2 に答える 2

5

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
于 2012-08-13T20:56:11.903 に答える
2

デーモンを作成するには、見つけたコードに示されているようにdouble fork()を使用します。次に、デーモンのinitスクリプトを作成し、それを/etc/init.d/にコピーする必要があります。

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

デーモンの自動起動方法を指定する方法はたくさんあります(例:chkconfig)。

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

または、特定のランレベルのシンボリックリンクを手動で作成することもできます。

最後に、サービスが予期せず終了したときにサービスを再起動する必要があります。/ etc/inittabにサービスのリスポーンエントリを含めることができます。

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

于 2012-08-13T21:25:51.520 に答える