之前都是以多以chkconfig来设置自启服务,但是在最近的一些debian发行版中,chkconfig被移除了,这里使用update-rc.d可以替代chkconfig
这里以设置nginx的自启服务为例
1.创建脚本
进入/etc/init.d
创建一个脚本名为nginx,然后
chmod 755 nginx
#!/bin/bash
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start nginx at boot time
# Description: Enable service provided by nginx.
### END INIT INFO
start()
{
/opt/nginx_1.8.0/sbin/nginx
exit 0;
}
stop()
{
/opt/nginx_1.8.0/sbin/nginx -s stop
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 0
;;
esac
exit 0
添加为系统服务
update-rc.d nginx defaults
从系统服务中删除
update-rc.d nginx remove
启动或停止
service nginx start
service nginx stop
或者
systemctl start nginx
systemctl start nginx
添加以及去除开机自启动
systemctl enable nginx
systemctl disable nginx