- #! /bin/sh
- # Author: rui ding
- # Modified: Geoffrey Grosenbach http://www.linuxidc.com
- # Modified: Clement NEDELCU
- # Reproduced with express authorization from its contributors
- set -e
- PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
- DESC="Nginx daemon"
- NAME=Nginx
- DAEMON=/usr/local/Nginx/sbin/$NAME
- SCRIPTNAME=/etc/init.d/$NAME
- # If the daemon file is not found,terminate the script.
- test -x $DAEMON || exit 0
- d_start() {
- $DAEMON || echo -n " already running"
- }
- d_stop() {
- $DAEMON –s quit || echo -n " not running"
- }
- d_reload() {
- $DAEMON –s reload || echo -n " could not reload"
- }
- case "$1" in
- start)
- echo -n "Starting $DESC: $NAME"
- d_start
- echo "."
- ;;
- stop)
- echo -n "Stopping $DESC: $NAME"
- d_stop
- echo "."
- ;;
- reload)
- echo -n "Reloading $DESC configuration..."
- d_reload
- echo "reloaded."
- ;;
- restart)
- echo -n "Restarting $DESC: $NAME"
- d_stop
- # Sleep for two seconds before starting again,this should give the
- # Nginx daemon some time to perform a graceful stop.
- sleep 2
- d_start
- echo "."
- ;;
- *)
- echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
- exit 3
- ;;
- esac
- exit 0
增加可执行权限
chmod a+x /etc/init.d/Nginx
2.利用sysv-rc-conf命令将其在对应rc?.d目录下建立一个软链接:
sysv-rc-conf Nginx on
该命令会在rc2.d ~ rc5.d目录下都建立了一个Nginx的软链接。
这里需要特别说明的是,Ubuntu系统下没有RedHat系统下的chkconfig命令。
但Ubuntu有一个类似的命令:sysv-rc-conf
。
通过apt-get命令完成sysv-rc-conf
软件的安装。
学习资料
Linux系统的运行级别有7个,分别对应的:
可以通过runlevel
命令来查看当前系统的运行等级:
其中第一个表示上一次的运行等级,N表示没有上一次运行等级的记录;第二个表示当前运行等级,这里为2.
linux中所有开机自启动项目运行脚本都放在/etc/init.d/
目录下;同时在/etc/
目录下有rc?.d目录,分别对应了7中不同的运行级别:
这里rc2.d目录就对应了我们系统当前的运行等级。
其中里面的一些文件其实都是/etc/init.d/
目录下文件的软链接:
整个开机自启动项的流程如下: