操作系统:Ubuntu 12.04
任务:在某些启动/停止条件下运行脚本。不会启动长期运行的服务/守护进程,仅启动短期操作。
新贵脚本test.conf:
description "test script"
start on local-filesystems or runlevel [2345]
stop on runlevel [!2345]
task
pre-start exec /full/path/to/script.sh start
pre-stop exec /full/path/to/script.sh stop
Run Code Online (Sandbox Code Playgroud)
问题:
原始任务可以使用新贵脚本解决吗?
谢谢。
更新。“事实上,您的 script.sh 具有启动和停止操作,这确实表明有一个长期运行的守护进程”
没有矛盾。有启动操作和清理操作,中间没有长时间运行的进程。
当我添加“exec”节时,即
exec /full/path/to/script.sh idle
Run Code Online (Sandbox Code Playgroud)
结果是这样的:
$ start test
test stop/waiting
$ status test
test stop/waiting
$ stop test
stop: Unknown instance:
Run Code Online (Sandbox Code Playgroud)
更正:节中提到的脚本是使用“start”和“idle”参数执行的,但不使用“stop”参数执行。
您的新贵配置文件缺少execorscript部分,并且您似乎只想使用预启动命令来启动,这是不正确的。根据新贵文档(http://upstart.ubuntu.com/getting-started.html):
所有作业文件必须具有 exec 或 script 节。这指定了作业将运行的内容。
可以指定额外的 shell 代码,使其在使用 exec 或 script 指定的二进制文件或脚本之前或之后运行。这些预计不会启动该过程,事实上,它们不能。它们的目的是准备环境并随后进行清理。
此外,您原来的任务请求的内容(“没有启动长时间运行的服务/守护程序,只有短期操作。”)似乎与您script.sh同时具有start和stop操作的事实相矛盾,这确实表明有一个长期运行的守护程序。
description "test script"
start on local-filesystems or runlevel [2345]
stop on runlevel [!2345]
task
# Assuming script.sh start starts, performs a process, and then exits,
# this should work
exec /full/path/to/script.sh start
# No need for a pre-stop
Run Code Online (Sandbox Code Playgroud)
我链接到的 upstart 文档据说已被弃用,您还可以查看http://upstart.ubuntu.com/cookbook/这是一个很好的参考,并且有有用的示例,所以如果事情仍然不清楚,我建议您阅读它。