linux sysvinit与upstart(2)

本文地址:http://blog.csdn.net/spch2008/article/details/9469775


有一个upstart的介绍,我觉得写的很清楚,值得一看。

这里采用的测试环境为ubuntu 12.04 server


Job:可以认为是一个服务或一个任务。可以通过配置文件来定义一个Job,如spch2008.conf,这样就定义了一个

名为spch2008的Job。该配置文件放置于/etc/init下。

linux sysvinit与upstart(2)_第1张图片


Script:配置文件中写入相应的脚本,完成指定功能。

#脚本格式
script
    # do some thing
    if [ ... ]; then
        ...
    fi
end script


pre-start script
    # prepare environment
    mkdir -p /var/run/foo
end script


Job States:状态迁移

Job State Transitions.

Current

State

Goal
start stop
waiting starting n/a
starting pre-start stopping
pre-start spawned stopping
spawned post-start stopping
post-start running stopping
running stopping pre-stop or stopping [11]
pre-stop running stopping
stopping killed killed
killed post-stop post-stop
post-stop starting waiting

假如当前状态处于waiting,执行start命令。首先由waiting转移到starting,然后starting==》pre-start==》spawned==》post-start==》running。

到达running状态,程序就运行起来了。

waiting : initial state.               
starting : job is about to start.      
pre-start : running pre-start section. 
spawned : about to run script or exec section.
post-start : running post-start section.
running : interim state set after post-start section processed denoting job is running 
pre-stop : running pre-stop section.
stopping : interim state set after pre-stop section processed.
killed : job is about to be stopped.
post-stop : running post-stop section.
从上述可以看出,pre-start状态对应于”pre-start script  end script“代码段,即处于pre-start状态时,正在执行pre-start script代码段。


实例:通过一个实例观察状态迁移过程。

#spch2008.conf
script
        mkdir -p /home/spch2008
        chown root:root /home/spch2008
end script
将上述代码保存为spch2008.conf,放置于/etc/init目录下。


执行:

root@nova-controller:/var/log# initctl log-priority debug

启动服务:

root@nova-controller:/var/log# status spch2008
spch2008 stop/waiting
root@nova-controller:/var/log# start spch2008
spch2008 start/running, process 22805
root@nova-controller:/var/log# 
上述过程在/home下创建了spch2008文件夹。

打开/var/log/syslog 看一下状态迁移情况。

root@nova-controller:/var/log# cat syslog  | grep init
很清楚的显示了各个状态的迁移情况。
init: spch2008 goal changed from stop to start
init: spch2008 state changed from waiting to starting
init: event_new: Pending starting event
init: Handling starting event
init: event_finished: Finished starting event
init: spch2008 state changed from starting to pre-start
init: spch2008 state changed from pre-start to spawned
init: spch2008 main process (22538)
init: spch2008 state changed from spawned to post-start
init: spch2008 state changed from post-start to running
init: event_new: Pending started event
init: Handling started event
init: event_finished: Finished started event
init: spch2008 main process (22538) exited normally
init: spch2008 goal changed from start to stop
init: spch2008 state changed from running to stopping
init: event_new: Pending stopping event
init: Handling stopping event
init: event_finished: Finished stopping event
init: spch2008 state changed from stopping to killed
init: spch2008 state changed from killed to post-stop
init: spch2008 state changed from post-stop to waiting
init: event_new: Pending stopped event
init: job_change_state: Destroyed inactive instance spch2008
init: Handling stopped event
init: event_finished: Finished stopped event

那么如何让程序可以在开机的时候自动运行呢?可以在rc.local中,写入相应的命令。但upstart可以通过事件完成。

start on runlevel [2345]
stop on runlevel [016]

run level在上一篇linux sysvinit已经说明了,上述命令在level2,3,4,5情况下启动,在0,1,6情况下关闭。


参考:http://upstart.ubuntu.com/cookbook/

你可能感兴趣的:(linux sysvinit与upstart(2))