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

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

 

posted on 2010-11-25 14:08  NanguoCoffee  阅读(790)  评论(0编辑  收藏  举报

导航