利用shell脚本个性化运行jar任务
利用shell脚本可以个性化运行jar任务,废话不多说,直接上代码:
#!/bin/bash
APP_PATH=/root/bigdata/jars/data_migration_from_sqlserver
APP_NAME=data_migration_from_sqlserver.jar
usage() {
echo "case: sh run.sh [start|stop|restart|status]"
exit 1
}
# 判断当前服务是否已经启动的函数
is_exist(){
#根据ps命令查询当前服务的进程号赋值给pid"
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
# pid等于null
return 1
else
# pid不等于null
return 0
fi
}
start(){
is_exist
# [$? -eq "0"] 说明pid不等于空,说明服务正在运行中,将进程号打印出来
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid=${pid}"
else
cd ${APP_PATH}
# nohup java -jar $APP_NAME > log.file 2>log.error & # 说明pid为空 执行java -jar 命令启动服务
nohup java -jar $APP_NAME >/dev/null 2>./log/log.error &
is_exist
echo "${APP_NAME} started. Pid=${pid}"
fi
}
stop(){
is_exist
# [$? -eq "0"] 说明pid不等于空 说明服务正在运行中,将进程号杀死
if [ $? -eq "0" ]; then
kill -9 $pid
echo "${APP_NAME} stopped. Pid=${pid}"
else
echo "${APP_NAME} is not running"
fi
}
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is not running"
fi
}
# 重启命令其实就是先执行关闭命令,再执行重启命令
restart(){
stop
start
}
# 这里的$1取的是当前输入命令的第二个参数start(sh start.sh start)
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
God will send the rain when you are ready.You need to prepare your field to receive it.