Ubuntu, upstart, and creating a pid for monitoring

依然范特西╮ 提交于 2019-11-28 14:45:05

问题


Below is a upstart script for redis. How to I create a pid so I use monit for monitoring?

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

回答1:


If start-stop-daemon is available on your machine, I would highly recommend using it to launch your process. start-stop-daemon will handle launching the process as an unprivileged user without forking from sudo or su (recommended in the upstart cookbook) AND it also has built in support for pid file management. Eg:

/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

Alternatively you could manually manage the pid file by using the post-start script stanza to create it and post-stop script stanza to delete it. Eg:

/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



回答2:


Egg's 1st example with start-stop-daemon is way to go.

If You choose 2nd, I would suggest $$ to obtain the 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


来源:https://stackoverflow.com/questions/9972023/ubuntu-upstart-and-creating-a-pid-for-monitoring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!