系统相关
首页 > 系统相关> > Shell脚本:开发服务启动脚本

Shell脚本:开发服务启动脚本

作者:互联网

本文使用的实验环境为:centos-6.10

实验服务为nginx服务

服务安装位置为/app/nginx-1.8.1,软连接为/app/nginx

当你编译安装了nginx服务后,想让服务像其他服务一样通过“/etc/init.d/服务”来控制服务的启动、停止。

可以通过一些思路来确定脚本的过程:

脚本内容:

#!/bin/bash
# chkconfig: 2345 40 98

path=/app/nginx/sbin
pid=/app/nginx/logs/nginx.pid
RETVAL=0
. /etc/init.d/functions

start(){
  if [ ! -f $pid ];then
    $path/nginx
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
      action "nginx is started" /bin/true
      return $RETVAL
    else
      action "nginx is started" /bin/false
      return $RETVAL
    fi
  else
    echo "nginx is running"
    return 0
  fi
}
stop(){
  if [ -f $pid ];then
    $path/nginx -s stop
    RETVAL=$?
    if [ $RETVAL -eq 0 ];then
      action "nginx is stopped" /bin/true
      return $RETVAL
    else
      action "nginx is stopped" /bin/false
      return $RETVAL
    fi
  else
    echo "nginx is no running"
    return $RETVAL
  fi
}
case "$1" in
  start)
    start
    RETVAL=$?
    ;;
  stop)
    stop
    RETVAL=$?
    ;;
  restart)
    stop
    sleep 1
    start
    RETVAL=$?
    ;;
  *)
    echo $"Usage:$0 {start|stop|restart}"
    exit 1
esca
exit RETVAL=$?

如果加入到开机自启:

chkconfig --add nginxd

注意脚本授权:

chmod +x /etc/init.d/nginxd

标签:脚本,bin,Shell,return,启动,stop,nginx,RETVAL,start
来源: https://blog.csdn.net/qq_42527269/article/details/110821017