配置 linux 快速 运行jar包

文件名 jun.sh



#!/bin/bash

#ps -aux | grep $1 >a.txt

process_name=$1
process_state=$2
process_nohup=$3

echo "【参数1:$0】 【参数2:$1】 【参数3:$2】 【参数4:$3】"

if [ "$1" == "w" ]; then
	if [ "$2" == "" ]; then
		echo "↓↓↓↓↓↓↓↓↓↓↓ tail -f nohup.out ↓↓↓↓↓↓↓↓↓↓"
		echo ""
		tail -f -n 100 nohup.out
	elif [ "$2" != "" ]; then
		echo "↓↓↓↓↓↓↓↓↓↓  tail -f $2  ↓↓↓↓↓↓↓↓↓↓" 
		echo ""
		tail -f -n 100 $2
	fi
	
fi

if [ "$process_name" == "setup" ]; then
	echo "设置别名"
	# 修改全局配置
	current_path=`pwd`
	chmod +x "$current_path/jun.sh"
	echo "" >> ~/.bashrc
	echo "alias jun.sh=$current_path/jun.sh" >> ~/.bashrc
	echo "alias cs='jun.sh'" >> ~/.bashrc
	echo "alias cs0='cs s 0'" >> ~/.bashrc
	echo "alias csstop='cs s 0'" >> ~/.bashrc
	echo "alias cs1='cs s 1'" >> ~/.bashrc
	echo "alias csquery='cs s 1'" >> ~/.bashrc
	echo "alias cs2='cs s 2'" >> ~/.bashrc
	echo "alias csstart='cs s 2'" >> ~/.bashrc
	echo "alias cs3='cs s 3'" >> ~/.bashrc
	echo "alias cs4='cs s 3 1'">> ~/.bashrc
	echo "alias cw='csw'" >> ~/.bashrc
	echo "alias csreload='cs s 3'" >> ~/.bashrc
	source ~/.bashrc

	exit 1
fi

exec_process(){
	if [ "$process_name" == "s" ]; then
		OLD_IFS="$IFS"
		IFS=$'\n'
		all_file_name=`ls -l |grep .jar| awk '{print $9}'`
		all_name=($all_file_name)
		length=${#all_name[@]}
		
		# 如果jar包数量为0,则退出执行
		if [ "$length" == "0" ]; then
			echo "好像木有jar包哦"
			IFS="$OLD_IFS"
			exit 1
		fi
		# =0: 结束所有jar
		if [ "$process_state" ==  0 ]; then
			for ((i=0;i<${#all_name[@]};i++));
			do
				kill_process ${all_name[$i]}
			done
		fi
		# =1:查询所有jar
		if [ "$process_state" == 1 ]; then
			for ((i=0;i<${#all_name[@]};i++));
			do
				query_process ${all_name[$i]}
			done
		fi
		# =2: 启动所有未启动的jar
		if [ "$process_state" == 2 ]; then
			for((i=0;i<${#all_name[@]};i++));
			do
				pid=`ps -ef | grep ${all_name[$i]} | grep -v "grep" |grep -v "/bin/bash"| awk '{print $2}'`	
				if [ "$pid" == "" ]; then
					start_process ${all_name[$i]}
				fi
			done
		fi
		# =3:重启所有jar
		if [ "$process_state" == 3 ]; then
			# 如果jar数量多于1,则提示选择需要执行的Jar
			if [ $length -gt 1 ]; then
				for((i=1;i<=${#all_name[@]};i++));
				do
					if [ "$i" == 1 ]; then
						echo "请选择要重启的jar包:"
						echo "     a:全部"
					fi
					echo "     $i:${all_name[ $i - 1 ]}"
				done
				read input
				if [ "$input" == "a" ]; then
					echo "即将重启/启动所有jar包"
					for((i=0;i<${#all_name[@]};i++));
					do
						kill_process ${all_name[$i]}
						start_process ${all_name[$i]}	
					done
				else
					exec_process_name=${all_name[ $input - 1 ]}
					echo "即将重启/启动$exec_process_name"
					kill_process $exec_process_name
					start_process $exec_process_name
				fi
			else
				# 如果jar只有一个,则直接结束并重启
				kill_process $all_name
				start_process $all_name
			fi
	
		fi
		# 还原IFS
		IFS="$OLD_IFS"
		# 退出
		exit 1
	fi
	
	# 帮助文档
	if [ "$process_name" == "" -a "$process_state" == "" ]; then
                echo "方式一  cs 进程名 [0:结束服务进程 1:查询服务进程 2:启动服务 3:重启服务 4:nohup启动Jar] w:跟踪阅读文件(默认nohup.out)"
		echo "方式二  csstop/cs0:结束当前目录所有服务  csquery/cs1:查询当前目录所有服务进程  csstart/cs2:启动当前目录所有未启动的服务  csreload/cs3:重启当前目录所有服务 cs4:nohup方式启动jar csw:跟踪阅读"
		echo "方式三 cw [文本名] :根据阅读文件"
		echo " "
		exit 1
	fi
	# 判断进程名是否为空
	if [ "$process_name" == "" ]; then
		echo "请输入进程名!"
		exit 1
	fi
	
	# 若操作值为空,则默认查询
	if [ "$process_state" == 1 ]; then
		query_process $process_name
		exit 1
	fi
	
        # 启动进程
        if [ "$process_state" == 2 ]; then
                start_process $process_name
                exit 1
        fi
	# 结束进程
	if [ "$process_state" == 0 ]; then
		kill_process $process_name
		exit 1
	fi
	# 重启进程
	if [ "$process_state" == 3 ]; then
		kill_process $all_pids
		start_process $process_name
		exit 1	
	fi
}	

# 结束进程
kill_process(){
	process_pids=`ps -ef | grep $1 | grep -v "grep" |grep -v "/bin/bash"| awk '{print $2}'`
		
	OLD_IFS="$IFS"
	IFS=" "
        pids=($process_pids)
        IFS="$OLD_IFS"
	
	for pid in ${pids[@]};do
		kill -9 $pid 1>/usr/null 2>&1
		echo "kill -9 $pid 完成"
	done
}
# 启动进程
start_process(){
	exec_name=$1
	is_jar=${exec_name#*.}
 	p_name=$1
	if [ "$is_jar" == "jar" ]; then
		java -jar $exec_name &
                echo "java -jar $exec_name & 成功"
		
	elif [ "p_name" != "" ]; then
		if [ "$process_nohup" == 1 ]; then
			echo "nohup java -jar $p_name >> nohup.out & 成功!"
			nohup java -jar $p_name >> nohup.out &
			exit 1
		elif [ "$process_nohup" == "" ]; then
                	java -jar ./$p_name &
                	echo "java -jar $exec_name & 成功"
		fi
	elif [ "$process_name" = "redis-server" ]; then
		/usr/bin/redis-server /etc/redis.conf &
                echo "redis-server 启动成功"
	fi
	
}
# 查看进程
query_process(){
	ps -aux | grep $1 | grep -v "grep" | grep -v "/bin/bash"
}

# 执行函数
exec_process

 

 

 配置 linux 快速 运行jar包_第1张图片

你可能感兴趣的:(配置 linux 快速 运行jar包)