1. 程式人生 > 其它 >一個常用的shell啟動程式的指令碼

一個常用的shell啟動程式的指令碼

技術標籤:linuxningto.com

一個常用的shell啟動程式的指令碼,指令碼、可執行程式、lib庫目錄在同級目錄即可,包括啟動、停止和檢視程序執行狀態。

指令碼名:svr.sh
應用程式名:gonetdisk
啟動:./svr gonetdisk start
停止:./svr gonetdisk stop
狀態: ./svr gonetdisk status

#!/bin/sh

set -x

ulimit -c unlimited

SOURCE=`pwd`

function start_server()
{
	SERVER_NAME=$1
	APP_PATH=$SOURCE/$SERVER_NAME
	export LD_LIBRARY_PATH=$SOURCE/../lib:$LD_LIBRARY_PATH
	process=`ps -ef | grep -w $APP_PATH | grep -v grep | awk '{print $2}'`;
    	if [ "$process" == "" ]; then
		nohup $APP_PATH >> nohup.out &
	else
		echo "already running, path: $APP_PATH"
	fi
}


function stop_server()
{
	SERVER_NAME=$1
	APP_PATH=$SOURCE/$SERVER_NAME
	process=`ps -ef | grep -w $APP_PATH | grep -v grep | awk '{print $2}'`;
	if [ "$process" != "" ]; then
		kill -9 $process
	else
		echo "is not running..."
	fi
}

function status_server()
{
	SERVER_NAME=$1
	SERVER_NAME=$1
	APP_PATH=$SOURCE/$SERVER_NAME
	process=`ps -ef | grep -w $APP_PATH | grep -v grep`;
	echo $process
}

case "$2" in
    start)
        start_server $1
        ;;
    stop)
        stop_server $1
        ;;
    status)
	status_server $1
	;;
    clear)
        ;;
    *)
        echo $"Usage: $0 process {start|stop|status|clear}"
        ;;
esac