Dan*_*bbs 4 startup shutdown upstart
我想foo do-startup-things在启动时foo do-shutdown-things运行,并在关闭时运行foo我自己的程序。
看起来 Upstart 是一个很好的候选者,但 Upstart 似乎是为与守护程序一起使用而设计的,因此运行service foo stop会产生错误,stop: Unknown instance:因为运行启动作业时执行的进程不再运行。
有没有办法在不启动守护进程的情况下使用 Upstart 在启动和关闭时执行任务?
对的,这是可能的。您应该定义两个任务作业,这是一个示例:
首先创建startTaskJob.conf:
# startTaskJob -
#
# This service print "script start" and end
description "print script start"
start on runlevel [2345]
task
console log
script
exec echo "script start"
end script
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方法对其进行测试:
sudo start startTaskJob
Run Code Online (Sandbox Code Playgroud)
并且输出将保存在 /var/log/upstart/startTaskJob.log
比创建stopTaskJob.conf:
# stopTaskJob -
#
# This service print "script stop" and end
description "print script stop"
start on runlevel [016]
task
console log
script
exec echo "script stop"
end script
Run Code Online (Sandbox Code Playgroud)
每次系统输入runlevel0、1 或 6时都会执行此脚本。在关闭时runlevel变为 0 并且 upstart init 进程将运行它,因为“在运行级别 [016] 上启动”。
你可以测试一下:
sudo start stopTaskJob
Run Code Online (Sandbox Code Playgroud)
更新: 这是关于如何在单个文件中执行此操作的示例。
# taskJob -
#
# This service print environment variable
# start on runlevel
description "print environment variable"
start on runlevel [0123456]
task
console log
script
if [ "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" -o "$RUNLEVEL" = "6" ]; then
exec echo "(stopTask) $UPSTART_EVENTS - $RUNLEVEL - job $UPSTART_JOB"
else
exec echo "(startTask) $UPSTART_EVENTS - $RUNLEVEL - job $UPSTART_JOB"
fi
end script
Run Code Online (Sandbox Code Playgroud)
我在 lubuntu 12.04 上测试过,这是/var/log/upstart/taskJob.log重启后的内容:
(stopTask) runlevel - 6 - job taskJob
(startTask) runlevel - 2 - job taskJob
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1237 次 |
| 最近记录: |