问题描述
下面是redis的新手脚本。如何创建一个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,我强烈建议您使用它来启动您的过程。 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
或者,您可以使用post-start script
节创建它来手动管理pid文件,并使用post-stop script
节来删除它。例如:
/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
次佳解决方案
Egg的第一个例子是start-stop-daemon。
如果你选择第二,我会建议$$来获得PID。
#!upstart
description "Redis Server"
env USER=redis
start on startup
stop on shutdown
respawn
script
echo $$ > /var/run/app_name.pid
exec sudo -u $USER sh -c "/usr/local/bin/redis-server /etc/redis/redis.conf 2>&1 >> /var/log/redis/redis.log"
end script
post-stop script
rm -f /var/run/app_name.pid
end script