Ubuntu14.04配置nginx开机自启动项

原文: 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命令来查看当前系统的运行等级:

1wds@wds-VirtualBox:~$ runlevel 2N 2

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

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

1wds@wds-VirtualBox:/$ ls /etc/ | grep ^rc 2rc0.d 3rc1.d 4rc2.d 5rc3.d 6rc4.d 7rc5.d 8rc6.d 9rc.local 10rcS.d

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

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

1wds@wds-VirtualBox:/etc/rc2.d$ ls -ltr 2total 4 3-rw-r--r-- 1 root root 677 313 2014 README lrwxrwxrwx 1 root root 18 128 19:49 S99rc.local -> ../init.d/rc.local lrwxrwxrwx 1 root root 18 128 19:49 S99ondemand -> ../init.d/ondemand lrwxrwxrwx 1 root root 18 128 19:49 S70pppd-dns -> ../init.d/pppd-dns lrwxrwxrwx 1 root root 19 128 19:49 S70dns-clean -> ../init.d/dns-clean lrwxrwxrwx 1 root root 15 128 19:49 S50saned -> ../init.d/saned lrwxrwxrwx 1 root root 27 128 19:49 S20speech-dispatcher -> ../init.d/speech-dispatcher lrwxrwxrwx 1 root root 15 128 19:49 S20rsync -> ../init.d/rsync lrwxrwxrwx 1 root root 20 128 19:49 S20kerneloops -> ../init.d/kerneloops lrwxrwxrwx 1 root root 21 129 17:25 S99grub-common -> ../init.d/grub-common lrwxrwxrwx 1 root root 15 129 17:45 S20nginx -> ../init.d/nginx lrwxrwxrwx 1 root root 17 129 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文件

1#! /bin/sh 2# Author: rui ding 3# Modified: Geoffrey Grosenbach http://www.linuxidc.com 4# Modified: Clement NEDELCU 5# 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
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )