本文要记述的是最简单的Ubuntu下开机自启 nginx的脚本

这里将nginx装在了/usr/local/nginx目录下,nginx本身没有注册成服务,所以直接使用服务开机自启是不行的,除非自己写nginx.service脚本,这不在本文范畴内。

创建脚本文件

$ sudo vim /etc/init.d/nginx.sh

脚本内容,注意替换root密码、nginx执行文件目录和配置文件目录

#!/bin/bash
#auto run nginx when system startup
sudo -S /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf << EOF
root密码
EOF
exit 0

指定开机自启,最后可以添加优先级,比如,90

$ sudo update-rc.d  nginx.sh defaults

此时重启就可以发现nginx已经开机自启了。

如果你在写完启动脚本的后,手动运行该脚本以确定是否可行的话,你会得到一个错误insserv: warning: script 'nginx.sh' missing LSB tags and overrides ,这种错误不会影响脚本的启动,只是提示脚本写的不规范,没有在脚本中发现以### BEGIN INIT INFO开头,以### END INIT INFO结尾的标签,因为没有影响,这里就没有写,可以参考下边的脚本去去除这个错误:

#!/bin/sh
# kFreeBSD do not accept scripts as interpreters, using #!/bin/sh and sourcing.
if [ true != "$INIT_D_SCRIPT_SOURCED" ] ; then
    set "$0" "$@"; INIT_D_SCRIPT_SOURCED=true . /lib/init/init-d-script
fi
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Example initscript
# Description:       This file should be used to construct scripts to be
#                    placed in /etc/init.d.  This example start a
#                    single forking daemon capable of writing a pid
#                    file.  To get other behavoirs, implemend
#                    do_start(), do_stop() or other functions to
#                    override the defaults in /lib/init/init-d-script.
### END INIT INFO

# Author: Foo Bar <foobar@baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

DESC="Description of the service"
DAEMON=/usr/sbin/daemonexecutablename

本文为实操笔记,转载请注明出处 https://www.cnblogs.com/hellxz/p/9441949.html

版权声明:本文为hellxz原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/hellxz/p/9441949.html