2分钟-实现开机nginx开机自启动脚本(shell篇)

如何快速写个启动脚本,主要通过如下几部

1,逻辑捋顺

  • 可以在txt文件中,已伪代码的方式,形成体系,罗列顺序,然后在一点点补充代码
  • 通过PID 进程文件,判断程序是否运行
  • 设置3个模块(开启,关闭,重新加载)
  • 然后在用case语句 去调用这个3个模块 实现启动脚本功能
  • restart看进程号变化,reload看配置文件是否生效

2,脚本主体内容

[[email protected]]# cat /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 32 62  #按照开机启动模板设定,32序列号 62系统等级
# description: Activates/Deactivates all network interfaces configured to  
[ -f /etc/init.d/functions ] &&  . /etc/init.d/functions  #引用系统函数库
nginx=/application/nginx/sbin/nginx
Pidfile=/application/nginx/logs/nginx.pid
oldboy(){
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
      action "Nginx is $1" /bin/true
    else
      action "Nginx is $1" /bin/false
    fi
}
##no.1 定义启动模块
Start(){
  if [ -f $Pidfile ];then
    echo "Nginx is running"
   else
    $nginx
    oldboy started
  fi   
  return $RETVAL
}
##no.2 定义关闭模块
Stop(){
  if [ ! -f $Pidfile ];then
    echo "nginx in not running"
    else
    $nginx -s stop
    oldboy stoped
  fi
}
##no.3 定义重新加载模块
Reload(){
  if [ ! -f $Pidfile ];then
    echo "Cat‘t open $Pidfile ,no such file or directory"
  else
    $nginx -s reload
    oldboy reloaed 
  fi
}
 
case "$1" in
    start)
    Start
;;
    stop)
    Stop
;;
    reload)
    Reload
;;
    restart)
    Stop
    sleep 2
    Start
;;
    *)
    echo "Usage: sh $0 {start|stop|reload|restart} "   
    exit 1
esac
exit   $RETVAL

3,把脚本放到/etc/init.d/ 下

3.1  查看服务自启动列表

[[email protected] init.d]# chkconfig --list|grep nginx
[[email protected] init.d]# chkconfig --list|grep mysql
mysqld         0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭

3.2 把脚本写入到/etc/init.d

[[email protected] init.d]# cd /etc/init.d/
[[email protected] init.d]# vi nginx   #内容见上面脚本

3.3 授权脚本 x执行权限

[[email protected] init.d]# chmod +x nginx 
[[email protected] init.d]# ll nginx 
-rwxr-xr-x 1 root root 1177 9月  26 15:45 nginx

4,添加开机自启动

[[email protected] init.d]# chkconfig nginx on
service nginx does not support chkconfig   ##报错,没有把nginx文件,添加到开机自启动里面
[[email protected] rc3.d]# chkconfig --list|grep nginx
nginx          	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭

4.1测试

[[email protected] rc3.d]# /etc/init.d/nginx start
Nginx is running

另外一种,开机自启动方法,可以把启动内容放到 /etc/rc.local文件中,主要要使用绝对路径

时间: 2024-08-13 18:35:33

2分钟-实现开机nginx开机自启动脚本(shell篇)的相关文章

Linux(CentOS)系统下设置nginx开机自启动

Nginx 是一个很强大的高性能Web和反向代理服务器.下面介绍在linux下安装后,如何设置开机自启动.首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:vi /etc/init.d/nginx 在脚本中添加如下命令:*************************************************************************************************************************

Ubuntu Nginx 开机自启动

update-rc.d命令,是用来自动的升级System V类型初始化脚本,简单的讲就是,哪些东西是你想要系统在引导初始化的时候运行的,哪些是希望在关机或重启时停止的,可以用它来帮你设置. 首先 我们需要在 /etc/init.d/目录下创建一个nginx的脚本文件,命令 vi nginx 输入以下内容 #! /bin/sh # Author: rui ding # Modified: Geoffrey Grosenbach http://www.linuxidc.com # Modified:

设置Nginx开机自启动

Nginx 是一个很强大的高性能Web和反向代理服务器.虽然使用命令行可以对nginx进行各种操作,比如启动等,但是还是根据不太方便.下面介绍在linux下安装后,如何设置开机自启动. 首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令: vim /etc/init.d/nginx 在脚本中添加如下命令: #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkcon

linux添加开机自启动脚本示例详解

来源: linux添加开机自启动脚本示例详解 linux下(以RedHat为范本)添加开机自启动脚本有两种方法,先来简单的; 一.在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,则:step1. 先修改好脚本,使其所有模块都能在任意目录启动时正常执行;step2. 再在/etc/rc.local的末尾添加一行以绝对路径启动脚本的行;如:$ vim /etc/rc.local#!/bin/sh## This script will be executed *after*

Fedora 22(15以上版本)开机自启动脚本

前段时间做了一个网站btdog磁力与btdog电视直播.DHT爬虫需要消耗比较多的资源,原来的服务器不够用了,于是自己使用电脑搭了一台服务器,使用Fedora22系统.在Fedora22中自动写了些开机自启动脚本,但始终找不到放在哪里.折腾了下,发现原来Fedora 从15开始,系统初始化软件开始由initscript转向了systemd方式,原来要写开机启动脚本一般写在rc.local里面,但现在rc.local已经不存在了,不过systemd仍然有rc-local服务. 编辑/usr/lib

Centos开机自启动脚本的制作

我的一个Centos开机自启动脚本的制作 一.切换到/etc/init.d/ 二.制作sh脚本 vi andy.sh [plain] #!/bin/sh #add for chkconfig #chkconfig: 2345 70 30 #description: the description of the shell   #关于脚本的简短描述 #processname: andyStart                    #第一个进程名,后边设置自启动的时候会用到 #下面要启动服务的命

linux添加开机自启动脚本

一.在/etc/rc.local中添加 如果不想将脚本粘来粘去,或创建链接什么的, 则: step1. 先修改好脚本,使其所有模块都能在任意目录启动时正常执行; step2. 再在/etc/rc.local的末尾添加一行以绝对路径启动脚本的行; 如: $ vim /etc/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own in

Linux学习篇之---centos中的开机自启动脚本

centos中的开机自启动脚本 在cenos环境下,安装jdk,需要配置环境变量,网上有人说: 1.直接将语句写入/etc/profile中 2.将语句写入一个java.sh文件,然后将文件放到/etc/profile.d/目录下 现在看一下/etc/profile文件中内容: 其实这两种效果是一样的,/etc/profile文件执行的时候会遍历/etc/profile.d/目录下的所有*.sh文件,并依次执行. 但是个人认为还是使用后者比较好: 因为如果用得着,就新建一个.sh文件,如果用不着

linux实现开机自启动脚本

Linux下(以RedHat为范本)添加开机自启动脚本有两种方法,先来简单的; 一.在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,则:step1. 先修改好脚本,使其所有模块都能在任意目录启动时正常执行;step2. 再在/etc/rc.local的末尾添加一行以绝对路径启动脚本的行;如:$ vim /etc/rc.local#!/bin/sh## This script will be executed *after* all the other init scr