#!/bin/sh #启动脚本 !/usr/bin/env bash !/bin/sh ROOT_DIR='/sdcard/server' APP_PATH=${ROOT_DIR}/safebox-edge-service PID_PATH=${ROOT_DIR}/service.pid LOG_PATH=${ROOT_DIR}/log CONFIG=${ROOT_DIR}/config.json #使用说明,用来提示输入参数 usage() { echo "Usage: sh 执行脚本.sh [start|stop|restart|status] [dev]|[test]|[prod]" exit 1 } #检查程序是否在运行 is_exist() { pid=$(ps -ef | grep $APP_PATH | grep -v grep | awk '{print $2}') #如果不存在返回1,存在返回0 if [ -z "${pid}" ]; then return 1 else return 0 fi } #启动方法 start() { if [ ! -d "${LOG_PATH}" ]; then mkdir ${LOG_PATH} fi if [ -f "$PID_PATH" ]; then echo "Service is already start ..." else echo "Service start ..." nohup ${APP_PATH} -c ${CONFIG} >${LOG_PATH}/log.out 2>&1 & printf '%d' $! >$PID_PATH echo "Service start SUCCESS " fi } #停止方法 stop() { if [ -f "$PID_PATH" ]; then kill -9 $(cat $PID_PATH) rm -rf $PID_PATH echo "Service is stop SUCCESS!" else pid=$(ps -ef | grep -i ${APP_PATH} | grep -v grep | awk -F" " '{print $2}') if [ ! -z $pid ]; then echo "Service is already stop ..." else kill -9 $pid echo "Service is stop SUCCESS!" fi fi } #输出运行状态 status() { if [ -f "$PID_PATH" ]; then pid=$(cat "$PID_PATH") pid=$(ps -ef | grep $pid | grep -v "grep" | awk -F" " '{print $2}') if [ -z "$pid" ]; then echo "${APP_PATH} is NOT running." else echo "${APP_PATH} is running. Pid is ${pid}" fi else is_exist if [ $? -eq "0" ]; then echo "${APP_PATH} is NOT running." else echo "${APP_PATH} is running. Pid is ${pid}" fi fi } #重启 restart() { stop start } if [ $2 ]; then CONFIG=$2 fi #根据输入参数,选择执行对应方法,不输入则执行使用说明 case "$1" in "start") start ;; "stop") stop ;; "status") status ;; "restart") restart ;; *) usage ;; esac
标签:脚本,服务,启动,PID,grep,pid,echo,fi,PATH From: https://www.cnblogs.com/uestc2007/p/17535088.html