tag: init upstart centos6.x 自启动 initctl event

CentOS6开始转用Upstart代替以往的init.d/rcX.d的线性启动方式。
upstart的概念就不科普了,简单介绍下:
upstart可以在以下情况start\stop服务:
1.runlevel改变时
2.收到事件时
3.startup,shutdown

在这之前先介绍下initctl。initctl可以做什么?

man initctl 可以看到:管理员可以使用initctl对upstart init deamon进行操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@aaaaa ~]# initctl help
Job commands:
  start                       Start job.
  stop                        Stop job.
  restart                     Restart job.
  reload                      Send HUP signal to job.
  status                      Query status of job.
  list                        List known jobs.
  
Event commands:
  emit                        Emit an event.
  
Other commands:
  reload-configuration        Reload the configuration of the init daemon.
  version                     Request the version of the init daemon.
  log-priority                Change the minimum priority of log messages from the init daemon
  usage                       Show job usage message if available.
  help                        display list of commands
  
For more information on a command, try `initctl COMMAND --help'.

我常用的主要是启动、停止、重启、list。

下面使用initctl list看看又哪些任务在运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@aaaa ~]# initctl list
mcu start/running, process 1639
rc stop/waiting
tty (/dev/tty3) start/running, process 3123
tty (/dev/tty2) start/running, process 3121
tty (/dev/tty6) start/running, process 3129
tty (/dev/tty5) start/running, process 3127
tty (/dev/tty4) start/running, process 3125
plymouth-shutdown stop/waiting
control-alt-delete stop/waiting
rcS-emergency stop/waiting
readahead-collector stop/waiting
kexec-disable stop/waiting
quit-plymouth stop/waiting
rcS stop/waiting
prefdm start/running, process 3116
init-system-dbus stop/waiting
readahead stop/waiting
splash-manager stop/waiting
start-ttys stop/waiting
readahead-disable-services stop/waiting
rcS-sulogin stop/waiting
serial stop/waiting

举个例子:mcu start/running, process 1639
可以看到mcu这个任务处于start并且running状态,并且进程号是1639.

那如何做一个自启动呢?了解完上面之后,下面就很简单了。

1
2
3
4
5
[root@aaaaa ~]# cd /etc/init
[root@aaaaa init]# ls
control-alt-delete.conf  mcu.conf                quit-plymouth.conf  rcS-emergency.conf        readahead.conf                   splash-manager.conf
init-system-dbus.conf    plymouth-shutdown.conf  rc.conf             rcS-sulogin.conf          readahead-disable-services.conf  start-ttys.conf
kexec-disable.conf       prefdm.conf             rcS.conf            readahead-collector.conf  serial.conf                      tty.conf

在/etc/init下可以看到有很多 .conf文件,每一个conf都是一个任务。
我们只要在这里添加一个conf,就可以注册一个任务。
下面的例子,我希望在runlevel为2345的时候,确保/tmp/test.sh一直在执行。
那么conf这样写:

1
2
3
4
5
6
7
[root@aaaaa init]# cat mcu.conf
start on runlevel 2
start on runlevel 3
start on runlevel 4
start on runlevel 5
respawn
exec /tmp/test.sh

解释:
respawn 是让任务反复启动
exec可以用来执行文件,如果要在conf里面写脚本,可以使用script...end script格式,这里就不做举例了。

到这里,就已经写好了conf。你可以使用initctl reload-configuration来重新加载任务。然后在initctl list中查看任务,是否mcu已经添加进去了。
如果没有添加进去,请检查conf中是否有语法错误。

如果添加进去了,任务应该是stop的状态,那么可以使用initctl start mcu来启动任务。
但是这样似乎有点麻烦,就把以上的都做成一个脚本吧:

1
2
3
4
5
6
#!/bin/bash
echo -e "start on runlevel 2\nstart on runlevel 3\nstart on runlevel 4\nstart on runlevel 5\nrespawn\nexec /tmp/test.sh" > /etc/init/mcu.conf
  
initctl reload-configuration
initctl list
initctl start mcu

执行脚本,就完成了添加conf和启动任务的操作。

以上只是一个简单的conf例子,如果想要在更多条件下启动、停止等等各种操作,可以参考man。
前面说到了initctl可以发送event,让任务启动,conf这样写:

1
2
3
4
start on evsolohac
script
echo “solohac~~~~~” >>/tmp/solohac.txt
end script

让这个任务启动的命令如下:

1
initctl emit evsolohac

centos6 自启动任务的更多相关文章

  1. Java面试知识点1

    typora-root-url: ......\Software\Typora\Picture Bean的作用域 在Spring的元素的scope属性设置bean的作用域,用来决定bean是单实例还是 ...

  2. Centos6.5 设置Tomcat8 service服务实现自启动和服务管理

    Centos6.5 设置Tomcat8 service服务实现自启动和服务管理 将tomcat设置成像apache,nginx一样. 用serviec xxxx start/stop/restart ...

  3. Linux相关问题-CentOS6.5 x64版本号下Tomcat无法自启动的解决的方法

    前段时间使用阿里云server.使用的是Linux CentOS6.5系统,在搭建完Tomcat后发现,Tomcat无法自启动. 将启动tomcat的命令为tomcat_home/bin/startu ...

  4. CentOS6.x生产环境下一键安装mono+jexus的脚本,自启动,带服务,版本号自控

    转自: http://linuxdot.net/bbsfile-3784 1.支持哪些个平台?答:暂时仅支持CentOS6.x平台,7.x未测试,欢迎测试并到群里反馈给我(昵称:无聊人士) 2.一键安 ...

  5. CentOS6.8下安装memcached并设置开机自启动

    参考资料:http://www.cnblogs.com/handongyu/p/6419305.html    http://coolnull.com/1986.html 一.安装libevent 首 ...

  6. CentOS6.8下安装redis并配置开机自启动

    参考资料:http://www.bubuko.com/infodetail-1006383.html   http://www.cnblogs.com/skyessay/p/6433349.html ...

  7. centos6.5_64bit-nginx开机自启动

    Nginx 是一个很强大的高性能Web和反向代理服务器.下面介绍在linux下安装后,如何设置开机自启动. 首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:   ...

  8. tomcat在centos6+上的自启动脚本

    #!/bin/bash # # tomcat startup script for the Tomcat server # # chkconfig: 345 80 20 # description: ...

  9. CentOS6设置开机自启动

    1.把开机启动脚本(mysqld)copy到文件夹/etc/init.d 或 /etc/rc.d/init.d 中 2.将启动程序的命令添加到 /etc/rc.d/rc.local 文件中,比如: # ...

随机推荐

  1. childNodes在IE与Firefox中的区别

    嗯,这是前几天写一个遍历双层List集合,动态输出对应的表格并且控制固定表头的效果时发现的一个知识点,程序编好后在IE8浏览器下测试没问题,在Firefox35.0.1总是报错,后来发现是IE与FF对 ...

  2. NULL值比较,两个列的合并,列值按条件替换。

    show create table 表名 -- 显示创建表的sql语句. 为已有的表增加新列.alter table 表名 add 列名 int NULL -- 此行加了一个int 类型 默认可以nu ...

  3. 使用JS对HTML标签进行增删改查

    以下为通过JS对li标签进行简单的增删改查: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...

  4. 【HTML/XML 12】URI、URN、URL的联系和区别

    导读:在学习XML的时候,书中有很多个地方都提到URL等几个概念,再之前做项目的时候,重定向或是转发时,也用到了这个URL,在学习Ajax时,ajax破坏了统一资源定位(URN)都或多或少的接触到了这 ...

  5. DOM 中 Property 和 Attribute 的区别

    原文地址:http://web.jobbole.com/83129/ property 和 attribute非常容易混淆,两个单词的中文翻译也都非常相近(property:属性,attribute: ...

  6. 设计模式-原型模式(Prototype)

    场景分析: 前面我们提到,交易对象Trade,还有继承他的债券交易BondTrade.期货交易FutureTrade. 现在有一个需求,需要提供方法将交易拆分成多笔小交易. 代码如下(如果没有clon ...

  7. MIPS平台移植apache 2.2.7

    参考文章: http://wenku.baidu.com/view/94e08a20a5e9856a561260e2.html http://httpd.apache.org/docs/2.4/ins ...

  8. 在信号处理函数中调用longjmp

    错误情况及原因分析 前两天看APUE的时候,有个程序要自己制作一个sleep程序,结果在这个程序中就出现了在信号处理函数中调用longjmp函数的情况,结果就出现了错误,具体错误是啥呢,请参见下面这段 ...

  9. 搭建高性能计算环境(一)、Linux操作系统的安装和配置

    一般课题组刚开始做计算,往往没有专门的集群,主要用自己的PC机.工作站或者买几台服务器来跑跑:小伙伴们摸索Linux的使用.编译一些开源软件.甚至写点Shell脚本需要耗费很多时间,耽搁了读文献.码论 ...

  10. 来,让我们谈一谈Normalize.css

    本文译自 http://nicolasgallagher.com/about-normalize-css/最初发布于我的博客:http://jerryzou.com/posts/aboutNormal ...