查看进程:
ps -ef|grep java
杀死进程:
kill -9 进程id
这里主要为shell脚本启动部署在服务器中jar包
这个是可以把配置文件yml放置在外面的。
#!/bin/bash#这里可替换为你自己的执行程序,其他代码无需更改APP_NAME=spott-api.jar #你的jar包#使用说明,用来提示输入参数usage() {echo "Usage: sh wss.sh [start|stop|restart|status]"exit 1}#检查程序是否在运行is_exist(){pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `#如果不存在返回1,存在返回0if [ -z "${pid}" ]; thenreturn 1elsereturn 0fi}#启动方法start(){is_existif [ $? -eq "0" ]; thenecho "${APP_NAME} is already running. pid=${pid} ."elsenohup java -jar $APP_NAME > catalina.out 2>&1 &echo "${APP_NAME} start success"fi}#停止方法stop(){is_existif [ $? -eq "0" ]; thenkill -9 $pidelseecho "${APP_NAME} is not running"fi}#输出运行状态status(){is_existif [ $? -eq "0" ]; thenecho "${APP_NAME} is running. Pid is ${pid}"elseecho "${APP_NAME} is NOT running."fi}#重启restart(){stopstart}#根据输入参数,选择执行对应方法,不输入则执行使用说明case "$1" in"start")start;;"stop")stop;;"status")status;;"restart")restart;;*)usage;;esac
其中demo.jar为我部署在服务器中的应用(demo.jar根据自己jar包名字更改)
启动 sh demo.sh start停止 shdemo.sh stop重启 sh demo.sh restart查看状态 sh demo.sh status
另一种形式: 这个是配置文件yml不放置外面的
#!/bin/bashcd `dirname $0`CUR_SHELL_DIR=`pwd`CUR_SHELL_NAME=`basename ${BASH_SOURCE}`JAR_NAME="zbgxgl.jar"JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME#JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m"JAVA_MEM_OPTS=""# SPRING_PROFILES_ACTIV="-Dspring.profiles.active=base"#SPRING_PROFILES_ACTIV=""LOG_DIR=$CUR_SHELL_DIR/logsLOG_PATH=$LOG_DIR/${JAR_NAME%.*}.logecho_help(){echo -e "syntax: sh $CUR_SHELL_NAME start|stop"}if [ -z $1 ];thenecho_helpexit 1fiif [ ! -d "$LOG_DIR" ];thenmkdir "$LOG_DIR"fiif [ ! -f "$LOG_PATH" ];thentouch "$LOG_DIR"fiif [ "$1" == "start" ];then# check serverPIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`if [ -n "$PIDS" ]; thenecho -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}."exit 1fiecho "Starting the $JAR_NAME..."# startnohup java $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV $JAR_PATH >> $LOG_PATH 2>&1 &COUNT=0while [ $COUNT -lt 1 ]; dosleep 1COUNT=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l`if [ $COUNT -gt 0 ]; thenbreakfidonePIDS=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'`echo "${JAR_NAME} Started and the PID is ${PIDS}."echo "You can check the log file in ${LOG_PATH} for details."elif [ "$1" == "stop" ];thenPIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`if [ -z "$PIDS" ]; thenecho "ERROR:The $JAR_NAME does not started!"exit 1fiecho -e "Stopping the $JAR_NAME..."for PID in $PIDS; dokill $PID > /dev/null 2>&1doneCOUNT=0while [ $COUNT -lt 1 ]; dosleep 1COUNT=1for PID in $PIDS ; doPID_EXIST=`ps --no-heading -p $PID`if [ -n "$PID_EXIST" ]; thenCOUNT=0breakfidonedoneecho -e "${JAR_NAME} Stopped and the PID is ${PIDS}."elseecho_helpexit 1fi
· ./start.sh start 启动
·./start.sh stop 暂停
输入 tail -f zbgxgl.log 即可查看输出日志
如果显示权限不够:
输入下面命令即可
chmod 777 ./*.sh
如果觉得《脚本启动jar包》对你有帮助,请点赞、收藏,并留下你的观点哦!