我们在启动一个java服务的时候,如果不使用命令,可以会进行以下操作
1. 后台启动一个服务
nohup + 参数 + jar包名字 + &
nohup java -jar -Xmx2048m -Xms2048m -XX:MaxNewSize=2048m -XX:MaxPermSize=2048m -XX:PermSize=2048M -XX:-UseGCOverheadLimit -Djava.awt.headless=true -verbose:gc -Xloggc:/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC xxxx.jar &
1.1 查看服务的状态
# 查看状态
ps -ef|grep xxx.jar
#获取端口
ps -ef| grep xxx.jar |grep -v grep |awk '{print $2}'
#查看nohup
tail -f nohup
2. 上面这个繁锁的命令,其实可以写成一个脚本,如下
#!/bin/bash
APP_NAME=xxx.jar
case $1 in
start)
nohup java -jar -Xmx2048m -Xms2048m -XX:MaxNewSize=2048m -XX:MaxPermSize=2048m -XX:PermSize=2048M -XX:-UseGCOverheadLimit -Djava.awt.headless=true -verbose:gc -Xloggc:/logs/gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC ${APP_NAME} &
echo ${APP_NAME} start!
;;
stop)
ps -ef| grep ${APP_NAME} |grep -v grep |awk '{print $2}' | sed -e "s/^/kill -9 /g" | sh -
echo ${APP_NAME} stop!
;;
restart)
bash "$0" stop
sleep 3
bash "$0" start
;;
status)
ps -aux | grep ${APP_NAME} | grep -v 'grep'
;;
log)
case $2 in
nohup)
tail -f nohup.out
;;
*)
echo "Example:bash services.sh log nohup" ;;
esac
;;
*)
echo "Example:bash services.sh [start|stop|restart|status]" ;;
esac
2.1 如果写成脚本以后就可以使用以下的命令了
# 启动服务
bash servcices.sh start/stop/start/status