Ubuntu,暴发户,并创build一个监控的pid

以下是redis的新贵脚本。 如何创build一个PID,所以我使用监控监视?

#!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" 

如果启动 – 停止守护进程在您的机器上可用,我强烈build议使用它来启动您的进程。 start-stop-daemon将会以非特权用户的身份启动进程,而不会从sudo或su( 在upstart cookbook中推荐 )分叉,而且它也支持pid文件pipe理。 例如:

/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节来手动pipe理pid文件来创build它,然后使用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 

蛋的第一个例子,开始 – 停止守护进程是要走的路。

如果你select2nd,我会build议$$来获得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