上一家公司就有这样一种程序,但我没去看,只是觉得太厉害了,后面自己写自己的项目时发现每次打包项目上线都要打开xftp上传jar包,然后再打开xshell来停止上一个版本的jar包,然后运行刚上传的jar包,这样太麻烦了,要是有些小bug没有发现然后运行到服务器才陆陆续续的出现,就要做这种打包,上传,运行的操作太多了,于是我就想到了编写shell脚本来启动jar包,后面想起上一家公司的运行jar包的方式就索性做了个这样的程序,目前初始还算过得去,后面还会进行修饰
1.先过一遍打包jar上传并运行的操作方法
打包 package
上传
用xftp上传
运行
Linux命令:
cd /usr/local/java //打开到你存放jar的目录下
ps -ef|grep java //查看Java进程,如果之前有运行的就执行,没有就不用
//假如说有,那么会输出类似下面的结果
root 4590 1 2 Feb28 ? 15:29:13 java -jar -Xms64M -Xmx128M -XX:PermSize=64M -XX:MaxPermSize=128m -Dspring.profiles.active=pro bk_ht_center.jar
root 30177 1 2 19:56 ? 00:00:28 java -jar -Xms64M -Xmx128M -XX:PermSize=64M -XX:MaxPermSize=128m -Dspring.profiles.active=pro article.jar
//第二个结果为pid,即4590 30177
//假如我们要运行的是article.jar这个jar包,那么停止这个进程
kill -9 30177 //杀死这个进程,当然这种写法不好,但是简单快速暴力,不好就是他是啥都不管直接关闭的,假如程序还有些其他事在做的话它也会直接关闭它,这样会导致数据损失,但通常来讲,既然要重新运行jar包,想必是没有其他的数据要处理
//假如说这个jar包之前没有运行或者已经停止掉了,那么输入一下命令
nohup java -jar -Xms64M -Xmx128M -XX:PermSize=64M -XX:MaxPermSize=128m -Dspring.profiles.active=pro article.jar > article.out &
nohup java -jar -Xms64M -Xmx128M -XX:PermSize=64M -XX:MaxPermSize=128m -Dspring.profiles.active=pro article.jar > > article.out & &
这段命令中 nohup 的作用是关闭会话不会停止Java进程,Java -jar是运行jar包, -Xms64M -Xmx128M -XX:PermSize=64M -XX:MaxPermSize=128m是指定jvm的一些内存大小,可以不写, -Dspring.profiles.active=pro是spring指定jar包运行时使用哪个配置文件的一个方式,> article.out & 是指定日志输出到> article.out 这个文件里面,最后一个&是引到下一个命令输入终端,省的占用命令窗口
2.使用shell脚本来运行jar包
摘抄网上以为老哥的方法:https://blog.csdn.net/bingxuesiyang/article/details/88531613
shell脚本编程可以看菜鸟:https://www.runoob.com/linux/linux-shell.html
#!/bin/bash
# 定义变量
# 要运行的jar包路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到
JAR_NAME="/xxx/xxx/xxx/xxx.war"
# 日志路径,加不加引号都行。 注意:等号两边 不能 有空格,否则会提示command找不到
LOG_PATh=/export/Logs/main.log
# 如果输入格式不对,给出提示!
tips() {
echo ""
echo "WARNING!!!......Tips, please use command: sh auto_deploy.sh [start|stop|restart|status]. For example: sh auto_deploy.sh start "
echo ""
exit 1
}
# 启动方法
start() {
# 重新获取一下pid,因为其它操作如stop会导致pid的状态更新
pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
# -z 表示如果$pid为空时执行
if [ -z $pid ]; then
nohup java -jar $JAR_NAME > /dev/null 2>&1 &
#{print $2}是输出结果第二个字段的值到pid中
pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
echo ""
echo "Service ${JAR_NAME} is starting!pid=${pid}"
echo "........................Here is the log.............................."
echo "....................................................................."
tail -f $LOG_PATh
echo "........................Start successfully!........................."
else
echo ""
echo "Service ${JAR_NAME} is already running,it's pid = ${pid}. If necessary, please use command: sh auto_deploy.sh restart."
echo ""
fi
}
# 停止方法
stop() {
# 重新获取一下pid,因为其它操作如start会导致pid的状态更新
pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
# -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
if [ -z $pid ]; then
echo ""
echo "Service ${JAR_NAME} is not running! It's not necessary to stop it!"
echo ""
else
kill -9 $pid
echo ""
echo "Service stop successfully!pid:${pid} which has been killed forcibly!"
echo ""
fi
}
# 输出运行状态方法
status() {
# 重新获取一下pid,因为其它操作如stop、restart、start等会导致pid的状态更新
pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
# -z 表示如果$pid为空时执行。注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
if [ -z $pid ];then
echo ""
echo "Service ${JAR_NAME} is not running!"
echo ""
else
echo ""
echo "Service ${JAR_NAME} is running. It's pid=${pid}"
echo ""
fi
}
# 重启方法
restart() {
echo ""
echo ".............................Restarting.............................."
echo "....................................................................."
# 重新获取一下pid,因为其它操作如start会导致pid的状态更新
pid=`ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}'`
# -z 表示如果$pid为空时执行。 注意:每个命令和变量之间一定要前后加空格,否则会提示command找不到
if [ ! -z $pid ]; then
kill -9 $pid
fi
start
echo "....................Restart successfully!..........................."
}
# 根据输入参数执行对应方法,不输入则执行tips提示方法
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
tips
;;
esac
这个老哥写的很好,虽然初看有些头痛,但仔细看是很简单的,就跟写Java一样能理解
将这些内容输入到test.sh中,然后放到Linux系统
可以照这个老哥的方式创建shell脚本,也可以照我的方式
cd /usr/local/java
touch test.sh
vi tesh.sh
按 i 编辑
然后将内容复制进去
命令操作
启动: sh test.sh start
关闭: sh test.sh stop
查看状态: sh tesh.sh status
重启: sh tesh.sh restart
还可能会用到一些常用的文件操作命令
https://blog.csdn.net/bicongcha5626/article/details/105051206/?utm_term