shell來start、stop、restart應用程序模板

如何實現 start 、stop、status?bash

1)啓動的時候將進程號寫入文件,中止的時候讀取文件獲得進程號;app

pstree pid -p | awk -F"[()]" '{for(i=0;i<=NF;i++)if($i~/[0-9]+/)print $i}' |grep -E -v "\{|\(|\["|xargs kill -9 rest

#!/bin/sh
BASE_HOME=/home/apple/test
PID=${BASE_HOME}/.pid
status(){
   echo "==========status======="
   status=`ps -p $$`
   

}

start() {
    echo "==========start==========="
}

stop() {
    echo "===========stop============"
    pid=$$
    kill_cmd="pstree -p $pid | awk -F \"[()]\" '{for(i=0;i<=NF;i++)if(\$i~/[0-9]+/)print \$i}' |"'grep -v -E "\[|\(|\)|\]"|xargs kill -9'
    eval ${kill_cmd}
}

restart() {
    stop;
    echo "sleeping.........";
    sleep 3;
    start;

}
case "$1" in
    'start')
        start
        ;;
    'stop')
        stop
        ;;
    'status')
        status
        ;;
    'restart')
        restart
        ;;
    *)
    echo "usage: $0 {start|stop|restart|status}"
    exit 1
        ;;
    esac
相關文章
相關標籤/搜索