linux下执行jar与关闭jar进程

脚本如下:

#端口号,根据端口号确定PID
PORT=8081
#启动命令所在目录
HOME='/usr/etc/server'
#查询监听PORT端口的程序,awk:过滤文本;cut-d / -f 1:以“/"分开的 第一个域
pid=`netstat -anp|grep :$PORT|awk '{printf $7}'|cut -d/ -f1`

start(){
	#判断pid非空,then
	if[-n "$pid"];then
		echo "server already start,pid:$pid"
		return 0
	fi
	#进入命令所在目录
	cd $HOME
	# nohup方式启动jar并将日志重定向到log中,2>&1:将stderr重定向到stdout中   &:放后台执行
	nohup java -jar UserInfoManager.jar > $HOME/UserInfoManager.log 2>&1 &
	echo "start at port:$PORT"
}

stop(){
	#判断pid是否为空
	if [ -z "$pid" ]; then
		echo "not find program on port:$PORT"
		return 0
	fi
	#结束进程
	kill -9 $pid
	echo "kill program ,pid:$pid"
}

status(){
	#判断pid是否为空
	if [ -z "$pid" ]; then
		echo "not find program on port:$PORT"
	else
		echo "program is running, pid:$pid"
	fi

}

#判断执行那个函数
case $1 in
		start )
		start
		;;
		stop )
		stop
		;;
		status )
		status
		;;
		*)
		echo "Usage: (start|stop|status)"
		;;
esac
exit 0

保存为test.sh


启动:./test.sh start

停止:./test.sh stop

查看状态: ./test.sh status


你可能感兴趣的:(linux)