1. 程式人生 > >shell 指令碼設定開機自啟動

shell 指令碼設定開機自啟動

#!/bin/bash

ProgramPath=/home/kent/StartMain
ARG=$1


FunStart(){                         #啟動程式的函式

if [ -e $ProgramPath ];then         #判斷可執行檔案是否存在
	pid=$(pidof ${ProgramPath}) #判斷這個程式是否已經啟動
	expr $pid + 0 >/dev/null
	if [ $? -eq 0 ] && [ $pid -gt 0 ];then 
			echo "StartMain process already exists."
			exit 0		
	fi
	nohup $ProgramPath > /dev/null 2>&1 &  #啟動程式
else
	echo $CollectorManger is not exists.
fi

}


FunStop(){                          #停止程式的函式
pid=$(pidof ${ProgramPath})         #獲取這個程式的PID
expr $pid + 0 >/dev/null           #判斷獲得的PID是否是整數,是否大於0
if [ $? -eq 0 ] && [ $pid -gt 0 ];then
	kill -9 $pid >/dev/null         #殺死該程式
fi

}


#根據傳進指令碼的引數分別執行對應的分支
case $ARG in
start):                            #啟動
FunStart

;;

stop):                             #停止
FunStop

;;

restart):                          #重啟
FunStop
FunStart

esac