支持start,stop,restart的运行脚本

本文介绍了一个用于Linux环境下管理Java应用程序的shell脚本。该脚本支持启动(start)、停止(stop)和重启(restart)操作,并允许配置最大堆内存。通过简单的命令行操作即可轻松管理Java进程。

linux下的运行脚本,支持start,stop,restart

使用说明;

1:

run.sh start

run.sh stop

run.sh restart

2:

   修改最大内存memlimit=512

项目名称:projectname=processServer
启动的类名:com.ifeng.ipicture.ProcessServer

 

 

run.sh

#!/bin/sh
#memory limit
memlimit=512M
projectname=processServer
ulimit -SHn 51200
dir=`dirname $0`
pidfile=pid
cd  $dir
CP=.:config/
for file in lib/*;
do CP=${CP}:$file;
done
retval=0
# start the server
start(){
        printf "Starting the server of $projectname\n"
        if [ -f "$pidfile" ] ; then
                pid=`cat "$pidfile"`
        printf 'Existing process: %d\n' "$pid"
                retval=1
        else
                java  -Xms512M \
                      -Xmx"$memlimit" \
                      -XX:+UseParallelGC \
                      -XX:+AggressiveOpts \
                      -XX:+UseFastAccessorMethods \
                      -Xloggc:logs/gc`date +%Y%m%d%H%M%S`.log \
                      -cp $CP com.ifeng.ipicture.ProcessServer  >>logs/log.log &
                echo $! >"$pidfile"
                if [ "$?" -eq 0 ] ; then
                        printf 'Done\n'
                else
                        printf 'The server could not started\n'
                        retval=1
                fi
        fi
}
# stop the server
stop(){
  printf "Stopping the server of $projectname\n"
  if [ -f "$pidfile" ] ; then
    pid=`cat "$pidfile"`
    printf "Sending the terminal signal to the process: %s\n" "$pid"
    PROCESSPID=`ps -ef|awk  '{print $2}'|grep "$pid"`
    if [[ $PROCESSPID -ne "$pidfile" ]] ; then
        rm -f "$pidfile";
        printf 'Done\n'
    fi
    kill -TERM "$pid"
    c=0
    while true ; do
      sleep 0.1
      PROCESSPID=`ps -ef|awk  '{print $2}'|grep "$pid"`
      if [[ $PROCESSPID -eq "$pidfile" ]] ; then
        c=`expr $c + 1`
        if [ "$c" -ge 100 ] ; then
          printf 'Hanging process: %d\n' "$pid"
          retval=1
          break
        fi
      else
        printf 'Done\n'
        rm -f "$pidfile";
        break
      fi
    done
  else
    printf 'No process found\n'
    retval=1
  fi
}
# dispatch the command
case "$1" in
start)
  start
  ;;
stop)
  stop
  ;;
restart)
  stop
  start
  ;;
hup)
  hup
  ;;
*)
  printf 'Usage: %s {start|stop|restart}\n'
  exit 1
  ;;
esac


# exit
exit "$retval"



# END OF FILEcho $CP

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值