在 11.10 启动时运行命令

Ind*_*ial 4 bash upstart init 11.10

所以我对如何在我的 Ubuntu 11.10 服务器上创建启动脚本感到困惑。我已经阅读了关于init-scripts、upstart-jobs 等的内容,但我读得越多,就越感到困惑。

我尝试了各种指南,但我还没有找到真正有效的人。

有人可以告诉我如何创建一个可以在 11.10 中运行的简单脚本吗?

Tum*_*oid 8

Marty Fried 的回答包含一个最有价值的信息:食谱。通读使您能够编写 init 脚本。

但是,弄乱 init.d、rc*.d、chkconfig 等并不是您想要做的。在 Ubuntu(和其他发行版)上,它们只是旧的 sysvinit 东西的残余,许多软件包仍在使用这些东西,或者只是出于遗留原因而支持。你不需要或不想去那里:-)

最简单的 Upstart 脚本就是启动一个守护进程(把它放在 /etc/init/mydaemon.conf 中):

exec /path/to/binary
Run Code Online (Sandbox Code Playgroud)

这就是你所需要的。这使得 Upstart 在你执行时运行守护进程start mydaemon

好的,您希望它自动启动吗?通常,在 dbus 之后开始是一个合乎逻辑的选择,所以让我们这样做:

start on started dbus
stop on stopping dbus
exec /path/to/binary
Run Code Online (Sandbox Code Playgroud)

这个简单的脚本会在 dbus 启动时启动您的守护进程,并且会在 dbus 停止之前停止它。

如果它崩溃,您希望它重生(重新启动)吗?没问题,只需将respawn自己的一行添加到文件中即可。

你的守护进程是分叉还是守护自己?好吧,让我们抓住它!expect fork在单叉的情况下添加,或者expect daemon在真正(双叉)守护进程的情况下添加。

让我们为您的守护进程总结一个简单的启动脚本:

author "Your name goes here - optional"
description "What your daemon does shortly - optional"

start on started dbus
stop on stopping dbus

# console output  # if you want daemon to spit its output to console... ick
respawn # it will respawn if crashed/killed

exec /path/to/binary
Run Code Online (Sandbox Code Playgroud)

如果您不想运行守护程序,而只想运行一系列命令,让我们忘记该exec行并添加一个脚本部分:

script
   echo "Hello world!"
end script
Run Code Online (Sandbox Code Playgroud)

这使得 Upstart 运行脚本而不是守护进程。脚本部分只是一个普通的 shell 脚本,所以你可以在里面做任何你想做的事情。

希望能帮助到你。坚持使用 Upstart 配置文件,不要弄乱旧的 sysv,你会保持理智:-)