一 启动jar包普通方式
1.将jar包打包好,上传服务器,并使用jar包的配置文件:application-dev.yml放到和jar包同一目录下,如下图所示

2. 启动命令:
nohup java -jar -Dfile.encoding=utf-8 -Dspring.config.location=/root/ljf-tmp/application-dev.yml pmsm-bloc-portal.jar > nohup.out 2>&1 &
二 使用shell脚本进行启动jar包
1.编写脚本内容:
#!/bin/bash
source /etc/profile
currTime=`date +"%Y-%m-%d %H:%M:%S"`
APP_NAME=pmsm-bloc-portal.jar
APP_DIR=/root/ljf-tmp
APP_CONFIG_FILE=application-dev.yml
#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
is_exist
if [ $? -eq 0 ]; then
echo "【 ${APP_NAME} 】is already running. pid=${pid}"
else
nohup java -jar -Dfile.encoding=utf-8 -Dspring.config.location=${APP_DIR}/${APP_CONFIG_FILE} ${APP_DIR}/${APP_NAME} > nohup.out 2>&1 &
pid=`ps -ef|grep java|grep $APP_NAME|awk '{print $2}'`
echo "${currTime}:start 【 $APP_NAME 】is successfully!!!,this app pid:${pid}"
fi
}
#程序主入口
start
2.赋予脚本执行权限
[root@km-003 ljf-tmp]# chmod 777 startup-jar.sh
3.执行脚本,启动jar包
[root@km-003 ljf-tmp]# ./startup-jar.sh
2022-10-24 22:02:37:start pmsm-bloc-portal.jar is successfully!!!,this app pid:2818983
三 使用shell脚本,进行传参启动,查看,停止
1.编写脚本内容
#!/bin/bash
source /etc/profile
#自定义配置
currTime=`date +"%Y-%m-%d %H:%M:%S"`
APP_NAME=pmsm-bloc-portal.jar
APP_DIR=/root/ljf-tmp
APP_CONFIG_FILE=application-dev.yml
#检查程序是否在运行
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
is_exist
if [ $? -eq 0 ]; then
echo "${APP_NAME} is already running. pid=${pid}"
else
nohup java -jar -Dfile.encoding=utf-8 -Dspring.config.location=${APP_DIR}/${APP_CONFIG_FILE} ${APP_DIR}/${APP_NAME} > nohup.out 2>&1 &
pid=`ps -ef|grep java|grep $APP_NAME|awk '{print $2}'`
echo "${currTime}:start $APP_NAME is successfully!!!,this app pid:${pid}"
fi
}
#停止方法
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
echo "${APP_NAME} is already shutdown"
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
sleep 5
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
2.赋予脚本执行权限
[root@km-003 ljf-tmp]# chmod 777 startup-jar.sh
3.执行脚本,启动jar包

脚本文件见:


本文介绍了如何使用Shell脚本来启动、停止和检查Java应用程序的状态。首先,通过命令行直接启动jar包,然后展示了一个简单的Shell脚本,用于实现同样的功能。接着,升级脚本,添加了参数支持,可以进行启动、停止和查看状态操作。通过赋予脚本执行权限并执行,可以方便地管理Java应用。
3976

被折叠的 条评论
为什么被折叠?



