Xiaopei's DokuWiki

These are the good times in your life,
so put on a smile and it'll be alright

User Tools

Site Tools


it:linux:upstart

Upstart

Upstart is an event-based replacement for the /sbin/init daemon which handles:

  1. starting of tasks and services during boot
  2. stopping them during shutdown
  3. and supervising them while the system is running.

关键配置

Upstart 里, stanza 1) == 配置.

一个基础的 upstart 配置如下:

$ cat /etc/init/cron.conf
# cron - regular background program processing daemon
#
# cron is a standard UNIX program that runs user-specified programs at
# periodic scheduled times
 
description	"regular background program processing daemon"
 
# 最普通的启动/关闭事件
start on runlevel [2345]
stop on runlevel [!2345]
 
# 文档后面有详述
expect fork
 
# 异常退出后会重启
respawn
 
# 启动的命令
exec cron
 
############################# EOF #############################
 
# ps 可看出进程在运行
$ ps aux  |grep cron
root       825  0.0  0.0  21328   404 ?        Ss   Apr01   0:05 cron
 
# upstart 也有 status 和 initctl list 可查看 service 的状态
$ status cron
cron start/running, process 825
$ initctl list | grep  cron
cron start/running, process 825

一些关键的配置如下.

expect

多实例相关: instance & task

如果要 使用不同配置开启一个服务的多个实例 (instance), 可如下操作 (例子为统一管理 node 相关的服务):

  1. 编写 instance 的 upstart conf
    instance.conf
    description 'node upstart script'
    author 'iain'
     
    start on (local-filesystems and net-device-up)
    stop on shutdown
     
    instance "Node - $NAME"
     
    respawn
    respawn limit 5 60
     
    script
      . /etc/node/$NAME.conf
      exec sudo -u node NODE_ENV=${NODE_ENV} /usr/bin/n use ${NODE_VERSION} ${NODE_PATH} >> ${LOG_PATH} 2>&1
    end script
  2. 编写每个 instance 的配置, 例如
    /etc/node/foo.conf
    NODE_VERSION="0.6.5"
    NODE_PATH="/var/application/test/app.js"
    LOG_PATH="/var/log/node/test.log"
    NODE_ENV="production"
  3. 启动某个 instance 的方法如下
    $ sudo start node NAME=foo
  4. 编写管理所有 instance 的 task 的 upstart conf
    task.conf
    description 'Start all node instances at boot'
    author 'iain'
     
    start on (local-filesystems and net-device-up)
     
    task
     
    script
      for file in `ls /etc/node/*.conf` ; do
        filename=`basename ${file%.*}`
        start node NAME=$filename
      done
    end script
  5. 这样, 因为 task 是遍历 /etc/node/*.conf, 所以只要在 /etc/node/ 下有 conf, 服务器启动时就能由 task 按该 conf 启动服务

参考: Upstart and Node.js | Clock

1)
诗节, a group of lines forming the basic recurring metrical unit in a poem; a verse.
it/linux/upstart.txt · Last modified: 2014/05/14 22:22 by admin