Linux 下Java 进程启动停止shell脚本

1.start.sh

appname=paas-portal
apppath=/data/docker/shared/apps/paas/$appname
configlocation=--spring.config.location=application-sit.yml
nohup java -Xmx500m -Xss64m -jar $apppath/$appname.jar $configlocation > $apppath/log.txt &

输入start.sh

2.stop.sh

输入stop.sh

ps -ef | grep java | grep paas-portal | cut -c 9-15 | xargs kill -9

3.jar.sh

#!/bin/bash
currenttime=$(date +%Y%m%d%H%M%S)
logfile="$1_${currenttime}.log"
jarfile="$1.jar"
command="java -Djava.security.egd=file:/dev/./urandom -Xms128m -Xmx512m -jar ${jarfile}"

start(){
    echo "INFO: Starting $jarfile ..."
    if [ "$logfile" != "" ]; then
        exec nohup $command > $logfile 2>&1 &
    else
        exec nohup $command &
    fi
    if [ "$1" == "nlog" ]; then
      echo "INFO: $jarfile started!"
      sleep 1
      echo "INFO: view process(name=$command) info ..."
      ps -ef | grep "$command"
      exit
    else
      echo "INFO: view $logfile ..."
      tail -f $logfile
    fi
}

stop(){  
 echo "INFO: stopping $jarfile ..."
 ps -ef | grep "$command" | awk '{print $2}' | while read pid  
 do 
    C_PID=$(ps --no-heading $pid | wc -l)
    if [ "$C_PID" == "1" ]; then
        kill -9 $pid
        echo "INFO: process(PID=$pid) end!"
    else  
            echo "WARN: process(PID=$pid) does not exist!"
    fi 
 done
 echo "INFO: $jarfile stopped!"
}

if [ $# == 0 ]; then
    echo 'ERROR: Invalid Parameter!'    
elif [ -f "$jarfile" ]; then
    if [ $# == 2 ] && [ "$2" == "nlog" ] || [ $# == 1 ]; then
            stop  
            start $2
    elif [ $# == 2 ] && [ "$2" == "stop" ]; then
        stop
        exit
    else
        echo 'ERROR: Invalid Parameter!'
    fi
else
    echo "ERROR: $jarfile does not exist!"
fi
echo 'Usage: jar.sh JAR_PREFIX [OPTION]'
echo '    JAR_PREFIX    jar file name prefix’
echo '    OPTION    stop or nlog'
echo 'Examples:'
echo '    jar.sh paas-bpm         --start or restart paas-bpm.jar, display log'
echo '    jar.sh paas-bpm nlog    --start or restart paas-bpm.jar, no display log'
echo '    jar.sh paas-bpm stop    --stop paas-bpm.jar’

启动输入 ./jar.sh jar包文件名
停止输入 ./jar.sh jar包文件名 stop

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值