下面是redis的upstart脚本。如何创建一个pid,所以我使用monit进行监控?
- #!upstart
- description "Redis Server"
- env USER=redis
- start on startup
- stop on shutdown
- respawn
- exec sudo -u $USER sh -c "/usr/local/bin/redis-server /etc/redis/redis.conf 2>&1 >> /var/log/redis/redis.log"
如果启动 – 停止 – 守护程序在您的机器上可用,我强烈建议使用它启动您的过程。 start-stop-daemon将处理作为非特权用户启动进程,而不从sudo或su(
recommended in the upstart cookbook)分支,并且它还内置支持pid文件管理。例如:
/etc/init/app_name.conf
- #!upstart
- description "Redis Server"
- env USER=redis
- start on startup
- stop on shutdown
- respawn
- exec start-stop-daemon --start --make-pidfile --pidfile /var/run/app_name.pid --chuid $USER --exec /usr/local/bin/redis-server /etc/redis/redis.conf >> /var/log/redis/redis.log 2>&1
或者,您可以使用启动后脚本节来创建pid文件并使用停止后脚本节来手动管理pid文件。例如:
/etc/init/app_name.conf
- #!upstart
- description "Redis Server"
- env USER=redis
- start on startup
- stop on shutdown
- respawn
- exec sudo -u $USER sh -c "/usr/local/bin/redis-server /etc/redis/redis.conf 2>&1 >> /var/log/redis/redis.log"
- post-start script
- PID=`status app_name | egrep -oi '([0-9]+)$' | head -n1`
- echo $PID > /var/run/app_name.pid
- end script
- post-stop script
- rm -f /var/run/app_name.pid
- end script