背景
在ubuntu下做开发,虚拟机要经常开启和关闭,重要的进程需要随机自启,非重要的可以手工启动。比如nginx就需要自启,confluence就没那么重要了。
为了控制哪些程序要自启,哪些程序不要自启,ubunt提供了一套API,update-rc.d。以下就是对于该API的操作示例和简单说明。
update-rc.d --help可查看更多用法。
===========================================
重要概念说明
luwenwei@ubuntu:~/dev/project$ ll /etc/rc?.d/*confluence1
lrwxrwxrwx 1 root root 21 Apr 27 2016 /etc/rc0.d/K20confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Apr 27 2016 /etc/rc1.d/K20confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Apr 27 2016 /etc/rc2.d/S20confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Apr 27 2016 /etc/rc3.d/S20confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Apr 27 2016 /etc/rc4.d/S20confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Apr 27 2016 /etc/rc5.d/S20confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Apr 27 2016 /etc/rc6.d/K20confluence1 -> ../init.d/confluence1*
confluence1是个shell脚本,rc*.d用来控制它的启不启动.
以/etc/rc0.d/K20confluence1为例,分解下/etc/rc[$a].d/$b$c$d,$a=0, $b=K, $c=20, $d=confluence1
$a: runlevel, [0-6]几个值, 0表示系统停机状态 1表示单用户或者系统维护状态 2-5表示多用户状态 6表示重启
$b: 启动/禁止启动, K/S,K表示开启,S表示停止;
$c: priority, 0-99 表示优先级, 1优先级最高,99优先级最低
$d: 服务执行脚本,shell脚本
查看某个服务:
ll /etc/rc?.d/*confluence1
lrwxrwxrwx 1 root root 21 Dec 15 11:57 /etc/rc0.d/K90confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Dec 15 11:57 /etc/rc1.d/K90confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Dec 15 11:57 /etc/rc2.d/S90confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Dec 15 11:57 /etc/rc3.d/S90confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Dec 15 11:57 /etc/rc4.d/S90confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Dec 15 11:57 /etc/rc5.d/S90confluence1 -> ../init.d/confluence1*
lrwxrwxrwx 1 root root 21 Dec 15 11:57 /etc/rc6.d/K90confluence1 -> ../init.d/confluence1*
更新某个服务:
luwenwei@ubuntu:~/dev/project$ sudo update-rc.d confluence1 start 90 2 3 4 5 . stop 90 0 1 6 .
System start/stop links for /etc/init.d/confluence1 already exist.
luwenwei@ubuntu:~/dev/project$ sudo update-rc.d confluence1 start 90 2 3 4 5 . stop 90 0 1 6 .
Adding system startup for /etc/init.d/confluence1 ...
/etc/rc0.d/K90confluence1 -> ../init.d/confluence1
/etc/rc1.d/K90confluence1 -> ../init.d/confluence1
/etc/rc6.d/K90confluence1 -> ../init.d/confluence1
/etc/rc2.d/S90confluence1 -> ../init.d/confluence1
/etc/rc3.d/S90confluence1 -> ../init.d/confluence1
/etc/rc4.d/S90confluence1 -> ../init.d/confluence1
/etc/rc5.d/S90confluence1 -> ../init.d/confluence1
删除某个服务:
luwenwei@ubuntu:~/dev/project$ sudo update-rc.d -f confluence1 remove
Removing any system startup links for /etc/init.d/confluence1 ...
/etc/rc0.d/K20confluence1
/etc/rc1.d/K20confluence1
/etc/rc2.d/S20confluence1
/etc/rc3.d/S20confluence1
/etc/rc4.d/S20confluence1
/etc/rc5.d/S20confluence1
/etc/rc6.d/K20confluence1
启动进程加入service服务
应用程序加入service服务后,可以使用系统的service命令完成开启,重启,结束等操作,很方便。
1,搞定应用程序的控制脚本;
2,将该脚本加入到/etc/init.d/目录中,并赋予执行权限:cp nginx /etc/init.d/; sudo chmod 755 /etc/init.d/nginx;
3,加入service中:sudo update-rc.d nginx defaults