原文: https://www.jianshu.com/p/2e03255cfabb

ubuntu配置开机自动启动服务

----------------------------------------------------------------

这里需要特别说明的是,Ubuntu系统下没有RedHat系统下的chkconfig命令。
但Ubuntu有一个类似的命令: sysv-rc-conf

通过apt-get命令完成sysv-rc-conf软件的安装。

背景

Linux系统的运行级别有7个,分别对应的:

  • 0: 关机
  • 1: 单用户(维护)
  • 2~5: 多用户
  • 6: 重启

可以通过runlevel命令来查看当前系统的运行等级:

wds@wds-VirtualBox:~$ runlevel
N 2

其中第一个表示上一次的运行等级,N表示没有上一次运行等级的记录;第二个表示当前运行等级,这里为2.

Linux中所有开机自启动项目运行脚本都放在/etc/init.d/目录下;同时在/etc/目录下有rc?.d目录,分别对应了7中不同的运行级别:

wds@wds-VirtualBox:/$ ls  /etc/ | grep ^rc
rc0.d
rc1.d
rc2.d
rc3.d
rc4.d
rc5.d
rc6.d
rc.local
rcS.d

这里rc2.d目录就对应了我们系统当前的运行等级。

其中里面的一些文件其实都是/etc/init.d/目录下文件的软链接:

wds@wds-VirtualBox:/etc/rc2.d$ ls -ltr
total 4
-rw-r--r-- 1 root root 677 3月 13 2014 README
lrwxrwxrwx 1 root root 18 12月 8 19:49 S99rc.local -> ../init.d/rc.local
lrwxrwxrwx 1 root root 18 12月 8 19:49 S99ondemand -> ../init.d/ondemand
lrwxrwxrwx 1 root root 18 12月 8 19:49 S70pppd-dns -> ../init.d/pppd-dns
lrwxrwxrwx 1 root root 19 12月 8 19:49 S70dns-clean -> ../init.d/dns-clean
lrwxrwxrwx 1 root root 15 12月 8 19:49 S50saned -> ../init.d/saned
lrwxrwxrwx 1 root root 27 12月 8 19:49 S20speech-dispatcher -> ../init.d/speech-dispatcher
lrwxrwxrwx 1 root root 15 12月 8 19:49 S20rsync -> ../init.d/rsync
lrwxrwxrwx 1 root root 20 12月 8 19:49 S20kerneloops -> ../init.d/kerneloops
lrwxrwxrwx 1 root root 21 12月 9 17:25 S99grub-common -> ../init.d/grub-common
lrwxrwxrwx 1 root root 15 12月 9 17:45 S20nginx -> ../init.d/nginx
lrwxrwxrwx 1 root root 17 12月 9 17:47 S20php-fpm -> ../init.d/php-fpm

整个开机自启动项的流程如下:

  1. 开机后,系统获得当前的运行等级(例如这里的等级为2);
  2. 运行/etc/rc?.d目录下的所有可执行文件(这里运行/etc/rc2.d/目录下所有的软链接。这些软链接的源文件都保存在/etc/init.d/目录下)。

因此我们只需要在/etc/init.d/完成启动nginx进程的脚本,然后在/etc/rc2.d/做对应的软链接即可。

配置nginx自启动文件

创建/etc/init.d/nginx文件

#! /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

然后利用sysv-rc-conf命令将其在对应rc?.d目录下建立一个软链接:

root@wds-VirtualBox:~# sysv-rc-conf nginx on

该命令会在rc2.d ~ rc5.d目录下都建立了一个nginx的软链接。

作者:北极狐狸
链接:https://www.jianshu.com/p/2e03255cfabb
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Ubuntu14.04配置nginx开机自启动项的更多相关文章

  1. Centos8配置Nginx开机自启动

    第一步:创建service文件,并编辑(可理解为开机时自动启动Nginx的脚本服务文件) vim /lib/systemd/system/nginx.service /lib 与 /usr/lib 里 ...

  2. Centos6.5 配置Nginx开机自启动

    1.在/etc/init.d/目录下创建 nginx 文件,内容如下: #!/bin/sh # # nginx - this script starts and stops the nginx dae ...

  3. Centos下安装并设置nginx开机自启动

    一.在centos环境下安装下载并安装nginx,由于nginx需要依赖一些环境才能安装,主要依赖g++.gcc.openssl-devel.pcre-devel和zlib-devel这些环境,首先得 ...

  4. centos7下安装、配置Nginx、设置Nginx开机自启动

    测试环境: [root@centos-linux ~]# cat /etc/redhat-releaseCentOS Linux release 7.6.1810 (Core) [root@cento ...

  5. Caffe+CUDA7.5+CuDNNv3+OpenCV3.0+Ubuntu14.04 配置参考文献 以及 常见编译问题总结

    Caffe+CUDA7.5+CuDNNv3+OpenCV3.0+Ubuntu14.04  配置参考文献 ---- Wang Xiao Warning: Please make sure the cud ...

  6. Caffe+CUDA8.0+CuDNNv5.1+OpenCV3.1+Ubuntu14.04 配置参考文献 以及 常见编译问题总结

    Caffe + CUDA8.0 + CuDNNv5.1 + OpenCV3.1 + Ubuntu14.04  配置参考文献 ---- Wang Xiao  Anhui University  CVPR ...

  7. ubantu18.04 配置nginx与uwsgi(前后端分离)

    ubantu18.04 配置nginx与uwsgi   一.首先先安装nginx静态服务 先更新 sudo apt-get update 1.安装gcc g++的依赖库 sudo apt-get in ...

  8. [MAC]配置Jenkins 开机自启动

    如果是将jenkins.war放在tomcat中运行的, 则可以配置开机启动tomcat,脚本如下: XXX表示是你安装Tomcat所在目录 #启动tomcat cd XXX/Tomcat8/bin ...

  9. Ubuntu14.04配置gcc4.4.4+Qt4.8.4交叉编译环境

    安装32位程序运行支持 sudo apt-get install lib32stdc++6 lib32z1 lib32ncurses5 lib32bz2-1.0 可能报错: lib32stdc++6 ...

随机推荐

  1. redux saga学习

    来源地址:https://www.youtube.com/watch?v=o3A9EvMspig Saga的基本写法 takeEvery与takeLatest的区别 takeEvery是指响应每一个请 ...

  2. 转:windbg常用命令

  3. summernote图片上传功能保存到服务器指定文件夹+php代码+java方法

    1.summernote富文本编辑器 summernote是一款基于bootstrap的富文本编辑器,是一款十分好用的文本编辑器,还附带有图片和文件上传功能. 那么在我们网站中想吧这个图片上传到服务器 ...

  4. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

  5. 洛谷——P1869 愚蠢的组合数

    P1869 愚蠢的组合数 题目描述 最近老师教了狗狗怎么算组合数,狗狗又想到了一个问题... 狗狗定义C(N,K)表示从N个元素中不重复地选取K个元素的方案数. 狗狗想知道的是C(N,K)的奇偶性. ...

  6. Linux命令之ping

    ping [选项] destination ping命令向网络主机发送ICMP回传请求 详细描述:ping使用ICMP协议强制ECHO_REQUEST(回传请求)数据报从主机或网关获取ICMP协议的E ...

  7. BZOJ 4025 二分图(时间树+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4025 [题目大意] 给出一张图,有些边只存在一段时间,问在一个每个时间段, 这张图是否 ...

  8. [TCO2013]DirectionBoard

    题意:给一个网格,每个格子有一个方向表示在这个格子上要往哪个方向走,你可以改变某些格子的方向,问最少多少次操作使得从任意格子出发都能回到这个格子 woc这都不会我还是回家种田去吧... 题目的要求是改 ...

  9. php应用数据库连接中的单例模式

    所谓的单例模式简而言之就是某个类在运行过程中只有一个实例,并且能够自行实例化并为整个系统的运行提供这个实例.在数据库连接中使用单例模式实例化数据库连接对象主要是可以避免重复的实例化对象而造成资源的浪费 ...

  10. centos6.5编译安装nginx

    系统64位 centos6.5 nginx官网:http://nginx.org/ 下载nginx源码包: wget  http://nginx.org/download/nginx-1.6.2.ta ...