Ubuntu,新贵,并创建一个监控pid

时间:2012-04-02 06:54:28

标签: ubuntu upstart

以下是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"

2 个答案:

答案 0 :(得分:68)

如果您的计算机上有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节来手动管理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

答案 1 :(得分:21)

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