常用命令:
$ sudo update-rc.d nginx defaults #增加服务
$ sudo update-rc.d -f nginx remove #移除服务
Linux服务的启动、停止和重载都是使用/etc/init.d/内的脚本。
在启动过程中或者改变运行级别( runlevel )时,/etc/rcX.d/内的相应的服务脚本被调用(这里的X是 runlevel number)
在Debian中安装新服务时,默认是开机启动的。例如,如果你安装了Nginx,Nginx服务会在下次开机时自启动。如果你不想Nginx开机自启动,你就要自己移除 /etc/rcX.d/SYYnginx 或使用 update-rc.d。而使用update-rc.d的优势很明显,它会自己移除、增加对/etc/init.d/内的链接。
下面以nginx为例,/etc/rcX.d文件如下:
$ ls -l /etc/rc?.d/*nginx lrwxrwxrwx 1 root root 15 1月 6 00:56 /etc/rc0.d/K20nginx -> ../init.d/nginx lrwxrwxrwx 1 root root 15 1月 6 00:56 /etc/rc1.d/K20nginx -> ../init.d/nginx lrwxrwxrwx 1 root root 15 1月 6 00:56 /etc/rc2.d/S20nginx -> ../init.d/nginx lrwxrwxrwx 1 root root 15 1月 6 00:56 /etc/rc3.d/S20nginx -> ../init.d/nginx lrwxrwxrwx 1 root root 15 1月 6 00:56 /etc/rc4.d/S20nginx -> ../init.d/nginx lrwxrwxrwx 1 root root 15 1月 6 00:56 /etc/rc5.d/S20nginx -> ../init.d/nginx lrwxrwxrwx 1 root root 15 1月 6 00:56 /etc/rc6.d/K20nginx -> ../init.d/nginx
对于runlevel 0,1和6链接以K开头,runlevel 2,3,4和5链接以S开头。这两个字母代表 Kill 和 Start 。
- Debian和Ubuntu中runlevel 2,3,4和5是多用户运行级别。
- 运行级别 0 是 停止
- 运行级别 1 是 单用户模式
- 运行级别 6 是 重启
移除服务(停止服务)
如果你想完全禁用Nginx,你需要删除所有 /etc/rcX.d/ 中的相关链接。但是如果使用 update-rc.d,如下:
$ sudo update-rc.d -f nginx remove
-f 参数:表示即使 /etc/init.d/nginx 文件还存在,也要强力移除链接
注意:如果下次Nginx升级了,那么本命令会失效。下面一条命令可以保证下次升级时,服务依旧不会重启
$ sudo update-rc.d nginx stop 70 0 1 2 3 3 4 5 6
增加服务
如果现在你又想Nginx开机自启动了,只要输入如a下命令即可:
$ sudo update-rc.d nginx defaults Adding system startup for /etc/init.d/nginx ... /etc/rc0.d/K20nginx -> ../init.d/nginx /etc/rc1.d/K20nginx -> ../init.d/nginx /etc/rc6.d/K20nginx -> ../init.d/nginx /etc/rc2.d/S20nginx -> ../init.d/nginx /etc/rc3.d/S20nginx -> ../init.d/nginx /etc/rc4.d/S20nginx -> ../init.d/nginx /etc/rc5.d/S20nginx -> ../init.d/nginx
自定义优先级
默认值是20,S20链接在S91链接前运行,K91在K20之前停止。
数字越小优先级越高,先运行、后停止。
为了设定启动和停止优先级为91,运行如下命令:
$ sudo update-rc.d nginx defaults 91 Adding system startup for /etc/init.d/nginx ... /etc/rc0.d/K91nginx -> ../init.d/nginx /etc/rc1.d/K91nginx -> ../init.d/nginx /etc/rc6.d/K91nginx -> ../init.d/nginx /etc/rc2.d/S91nginx -> ../init.d/nginx /etc/rc3.d/S91nginx -> ../init.d/nginx /etc/rc4.d/S91nginx -> ../init.d/nginx /etc/rc5.d/S91nginx -> ../init.d/nginx
对启动和停止设定不同的优先级
下面我们设定启动优先级为20,停止优先级为80
$ sudo update-rc.d nginx defaults 20 80 Adding system startup for /etc/init.d/nginx ... /etc/rc0.d/K80nginx -> ../init.d/nginx /etc/rc1.d/K80nginx -> ../init.d/nginx /etc/rc6.d/K80nginx -> ../init.d/nginx /etc/rc2.d/S20nginx -> ../init.d/nginx /etc/rc3.d/S20nginx -> ../init.d/nginx /etc/rc4.d/S20nginx -> ../init.d/nginx /etc/rc5.d/S20nginx -> ../init.d/nginx
完全自定义运行级别和优先级
设定启动运行级别2,3,4和5的优先级为20,停止运行级别为0,1和6的优先级为80
$ sudo update-rc.d nginx start 20 2 3 4 5 . stop 80 0 1 6 . Adding system startup for /etc/init.d/nginx ... /etc/rc0.d/K80nginx -> ../init.d/nginx /etc/rc1.d/K80nginx -> ../init.d/nginx /etc/rc6.d/K80nginx -> ../init.d/nginx /etc/rc2.d/S20nginx -> ../init.d/nginx /etc/rc3.d/S20nginx -> ../init.d/nginx /etc/rc4.d/S20nginx -> ../init.d/nginx /etc/rc5.d/S20nginx -> ../init.d/nginx
设定启动运行级别2,3和4的优先级为20,运行级别5的优先级为30。停止运行级别0,1和6的优先级为80
$ sudo update-rc.d nginx start 20 2 3 4 . start 30 5 . stop 80 0 1 6 . Adding system startup for /etc/init.d/nginx ... /etc/rc0.d/K80nginx -> ../init.d/nginx /etc/rc1.d/K80nginx -> ../init.d/nginx /etc/rc6.d/K80nginx -> ../init.d/nginx /etc/rc2.d/S20nginx -> ../init.d/nginx /etc/rc3.d/S20nginx -> ../init.d/nginx /etc/rc4.d/S20nginx -> ../init.d/nginx /etc/rc5.d/S30nginx -> ../init.d/nginx
注意:命令行中得点好“.”不能少。
参考:
Linux/CentOS 服务安装/卸载,开机启动chkconfig命令详解|如何让MySQL、Apache开机启动?
http://www.debuntu.org/how-to-managing-services-with-update-rc-d