Linux系统设置开机启动有很多方法,网上也有许多详细教程。本文只关注用 update-rc.d 命令给 Debian 添加开机启动。

例如:将test.sh脚本添加到开机自启。

1.将 test.sh 脚本放到 /etc/init.d/ 目录下

cp test.sh /etc/init.d/  
cd /etc/init.d/  
chmod +x  test.sh  

2.设置开机自启

update-rc.d test.sh defaults  

运行 update-rc.d 很可能会出现错误提示:

insserv: warning: script 'test.sh' missing LSB tags and overrides  

这是因为 test.sh 不符合 debian 开机自启文件的内容规范,debian 要求文件头部有启动信息。参考同目录下的 /etc/init.d/skeleton 文件头,把以下内容复制到 test.sh 再运行 update-rc.d test.sh defaults

#!/bin/sh
### BEGIN INIT INFO
# Provides:          test
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start:        2 3 4 5
# Default-Stop:         0 1 6
# Short-Description: test
# Description: test
### END INIT INFO

3.删除开机启动

update-rc.d -f test.sh remove  

via:用update-rc.d命令给Debian添加开机启动项