linux服务启动脚本实例

#!/bin/bash
#chkconfig: 2345 80 05

PID=/var/run/$(basename $0).pid
#Environment="PATH=/opt/py3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"

status() {
        if [ -f $PID ];then
          echo "jumpserver is running"
        else
          echo "jumpserver is not running"
        fi
}
start() {
        if [ -f $PID ];then
          echo "jumpserver is already started"
        else
          cd /opt/jumpserver/jumpserver
          ./jms start all -d      #python
          sleep 3
          cd /opt/coco
          ./cocod start  -d       #python
          sleep 3
          service redis start
          /opt/onesql5718/bin/mysqld_safe &
          service docker start
          docker start bd23ce6583f5
          service nginx start
          echo $$ > $PID
        fi
}
stop() {
        if [ -f $PID ];then
          service redis stop
          kill -9 `ps -ef | grep -v grep | grep mysqld_safe | awk '{print $2}'`
          docker stop bd23ce6583f5
          service docker stop
          cd /opt/jumpserver/jumpserver
          ./jms stop all -d
          cd /opt/coco
          ./cocod stop  -d
          service nginx stop
          rm -fr $PID
        else
          echo "jumpserver is already stopped"
        fi
}
case "$1" in
        "start")
          start
          ;;
        "stop")
          stop
          ;;
        "restart")
          stop
          start
          ;;
        "status")
          status
          ;;
        *)
          echo "Usage:$0{start|stop|restart|status}"
          exit 1
          ;;
esac

exit 0

$$:代表所在命令的PID

$(basename $0):运行的脚本名称

$!:代表最后执行的后台命令的PID

$?:代表上一个命令执行是否成功的标志,如果执行成功则$? 为0,否则不为0

$(dirname $0) :当前Shell程序的目录

posted @ 2020-02-26 14:49  Me-lihu  阅读(1)  评论(0编辑  收藏  举报