在我们日常开发中,需要将写好的java代码打jar包,然后上传到服务器上进行运行
使用此脚本可以简化对程序 启动、重启、停止等工作的重复操作
#!/bin/bash
app='demo.jar'
args='-Xms512m -Xmx512m'
cmd=$1
pid=`ps -ef |grep $app | grep -v 'grep'| awk '{print $2}'`
startup(){
#nohup java -jar $args $app > logs/server.log &
nohup java -jar $app >/dev/null 2>&1 &
}
if [ ! $cmd ]; then
echo "Please specify args 'start|restart|stop|status'"
exit
fi
if [ $cmd == 'start' ]; then
if [ ! $pid ]; then
startup
else
echo "$app is running! pid=$pid"
fi
fi
if [ $cmd == 'status' ]; then
if [ $pid ]; then
echo "$app is running! pid=$pid"
else
echo "$app is stop! "
fi
fi
if [ $cmd == 'restart' ]; then
if [ $pid ]
then
echo "$pid will be killed after 2 seconds!"
sleep 2
kill -9 $pid
fi
startup
fi
if [ $cmd == 'stop' ]; then
if [ $pid ]; then
echo "$pid will be killed after 2 seconds!"
sleep 2
kill -9 $pid
fi
echo "$app is stopped"
fi
使用说明:
-
脚本中修改参数
app:jar包名
args:jvm启动参数 -
服务器中修改脚本权限
chmod 755 server.sh -
格式化字符
sed -i ‘s/\r$//’ server.sh -
使用
sh server.sh start
sh server.sh restart
sh server.sh status
sh server.sh stop