我发现帮助处理init.d脚本的最好工具是“start-stop-daemon”。它将运行任何应用程序,监视run/pid文件,在必要时创建它们,提供停止守护进程的方法,设置进程用户/组id,甚至可以设置进程的后台。
例如,这是一个可以启动/停止wsgi服务器的脚本:#! /bin/bash
case "$1" in
start)
echo "Starting server"
# Activate the virtual environment
. /home/ali/wer-gcms/g-env/bin/activate
# Run start-stop-daemon, the $DAEMON variable contains the path to the
# application to run
start-stop-daemon --start --pidfile $WSGI_PIDFILE \
--user www-data --group www-data \
--chuid www-data \
--exec "$DAEMON"
;;
stop)
echo "Stopping WSGI Application"
# Start-stop daemon can also stop the application by sending sig 15
# (configurable) to the process id contained in the run/pid file
start-stop-daemon --stop --pidfile $WSGI_PIDFILE --verbose
;;
*)
# Refuse to do other stuff
echo "Usage: /etc/init.d/wsgi-application.sh {start|stop}"
exit 1
;;
esac
exit 0
您还可以看到一个如何将它与virtualenv一起使用的示例,我将一直推荐这个示例。