Linux Shell's Chatterserver service control script

Source: Internet
Author: User
Tags usleep

This script is a Chatterserver service control script that can control Chatterserver start, stop, reboot, and Status View through service commands, as if MySQL had/etc/init.d/mysql or/etc/init.d/ Like Mysqld, just this Chatterserver service control script is more difficult to write, and some information is more complex to capture and judge, because it is mainly related to the operation of Chatterserver and some of the things that happen during startup, This is well-marked in the script's comments.

Because Chatterserver is running on Ubuntu, there is a big difference with centos, such as sleep 1 and usleep 100000, where centos can control more accurate delays, Ubuntu cannot support Usleep and therefore cannot control more accurate delays, leading to potential performance problems.

To facilitate reading, here are a few examples of questions:

1.ChatterServer Java Core command line after execution, may be due to program or performance reasons, will delay the establishment of port connections;

2.ChatterServer jar package contains some configuration files, in the specific directory, you must first enter these directories in order to execute the Java Core command line;

Problem Solving methods:

For the first problem, the method of multiple judgment and delay execution is adopted.

For the second problem, debugging brought more problems, and later only with the development of communication, to understand its reasons;

This script is also based on the requirements of the development, the Chatterserver log is saved by date to facilitate the change in service status when the new log overwrite, the script run log to the new log, a total of management view.

The following is the script version fourth:

#!/bin/bash#chkconfig: 345 86 14#description: startup and shutdown script  for chatterserverversion=1.0.0-snapshotbasedir=/data/chatterserverlogdir= $BASEDIR/logsserviceport= 29092pidfile= $BASEDIR/pid/chatter.pidserver= $BASEDIR/chatter-$VERSION \.jarbasename=chatter# -xms2g - Xmx2g -xmn2g -xss128k -xx:maxpermsize=64m -xx:-useparallelgc -xx:+useparalleloldgc  -XX:PARALLELGCTHREADS=4 -XX:+USECONCMARKSWEEPGC -XX:MAXTENURINGTHRESHOLD=30 -XX: Survivorratio=6args= "" Status ()  {    # the judgment priority: pid  > port > piffile    # netstat run by common  user will get some error output, so we put those error  outout to /dev/null    if [[ $ (Netstat -anop 2>/dev /null | grep  $SERVICEPORT  | grep listen)  | |  -f  $PIDFILE  ]];then         #pid =$ (cat  $PIDFILE) pid=$ (ps -ef | grep java | grep  $BASENAME  | grep -v  grep | awk  ' {print $2} ')         if [[ $ pid !=  ""  && $ (ps -ef | grep  $pid  | grep -v  GREP)  ]]; then            echo   "Success: chatterserver is ok"              exit 0        else             echo  "Error: chatterserver pid is not  exist "            exit 1        fi    elif [[ ! $ ( netstat -anop 2>/dev/null | grep  $SERVICEPORT  | grep listen)  ] ]; then            echo  "ERROR:  Chatterserver port is not listen "             exit 1    elif [[ ! -f  $PIDFILE  ]];  then        echo  "Error: chatterserver pid file  is not exist "        exit 1     else        echo  "Error:  chatterserver is  not running "        exit 1    fi} Start ()  {    if [[ -e  $PIDFILE  ]]; then        echo   "error: pidfile  $PIDFILE  exist, server has started with pid  $ (cat  $PIDFILE) "        # pid file can  be deleted        /bin/rm -f  $PIDFILE          exit 1    fi    if [[ -e   $SERVER  ]]; then        echo  "info: starting  chatterserver "        # start chatterserver core  daemon        # Why using  "date +"%y%m%d ""?  Because we just need restart this once per day# For  Chatterserver wiil find some file in  $BASEDIR         cd $ basedir#nohup java -jar  $SERVER   $ARGS  >> $LOGDIR/console-$ (date + "%y%m%d"). out 2>&1 &        java -jar  $SERVER   $ARGS  >> $LOGDIR/console-$ (date + "%y%m%d") .out 2>&1 &          #java  -jar  $SERVER   $ARGS  > $LOGDIR/console.out  2>&1 &        retval=$?# shell do not  need home directory        ## For  chatterserver wiil find some file in  $BASEDIR #cd          if [[  $RETVAL  -eq 0 ]]; then             ## $! --> expands to the process id of the  most recently executed background  (asynchronous)  command.              #echo  $! >  $PIDFILE              # for java performance issue, port  29092 will listen latter, we will waiting for 2 second             sleep 2             # get pid var             # TODO remove debug info              #echo   "debug: "             &nbsp, #ps  -ef | grep  $BASENAME  | grep -v grep | awk  ' { print $2} '             # end debug             pid=$ (Ps -ef | grep  java | grep  $BASENAME  | grep -v grep | awk  ' {print  $2} ')             # send pid  Number to pid file            echo   $pid  >  $PIDFILE             #  Those lines will remove in next release             # TODO remove debug info              #echo   "debug: live 1"              # for java performance issue, port 29092 will listen  latter, so we change judgment conditions             if [[ $ (Netstat -anop 2>/dev/null | grep   $SERVICEPORT  | grep listen)  | |  -f  $PIDFILE  ]]; then                 echo  "Success: chatterserver start ok"                  # Setting up  start log                  echo  "[ $ (date +"%d %t ")  ] success: chatterserver started with pid $ (cat  $PIDFILE)    >> $LOGDIR/service.log             fi             # TODO remove debug info              #echo   "Debug: live 2"              # -those lines will remove in next  release             #echo   "SUCCESS:  chatterserver start ok "             ## Setting up start log               #echo   "[ $ (date +"%d %t ")  ] SUCCESS: ChatterServer  started with pid $(cat  $PIDFILE)    >> $LOGDIR/service.log         else            echo  "ERROR:  Chatterserver start failed "            #  Setting up start log              echo  "[ $ (date +"%d %t ")  ] error: chatterserver start  failed  " >> $LOGDIR/service.log             exit  $RETVAL         fi     else        echo  "Error: couldn ' t find  $SERVER"         # todo we just think this is  not essential        # do not setting up log here         exit 1    fi}stop ()  {    if [[  -e  $PIDFILE  ]]; then        pid=$ (cat $ Pidfile)          #if  kill -TERM  $PIDFILE  >/dev/ null 2>&1        # todo remove debug  info         #echo   "debug:  $LOGDIR/console-$ (date +"%Y%m %d "). Out"         # Ubuntu can NOT use  "Usleep ", so use " Sleep " instead        # usleep  100000         if kill -TERM  $pid  > > $LOGDIR/console-$ (date + "%y%m%d") .out && sleep 1             then            echo  "Success: chatterserver stop ok with term"              # Setting up stop log              echo  "[ $ (date +"%d %t ")  ] success:  ChatterServer stop OK with TERM  " >> $LOGDIR/service.log             # because we can not use  usleep , so we must comment out sleep 1 next              #sleep  1             # ubuntu can not use  "Usleep", so use  "Sleep"  instead             # usleep 100000          elif kill -KILL  $pid  >/dev/null 2> &1 && sleep 1             then            echo  "success:  Chatterserver stop ok with kill "             # Setting up stop log              echo  "[ $ (date +"%d %t ")  ] SUCCESS:  chatterserver stop ok with kill  " >> $LOGDIR/service.log             # because we can not use usleep , so we must  comment out sleep 1 next               #sleep  1        else             echo  "Error: chatterserver stop faild"             # Setting up stop  log             echo  "[ $ (date  + "%d %t")  ] ERROR: ChatterServer stop failed  " >> $LOGDIR/ service.log            exit 1         fi        # remove pid  file        if [[ -f  $PIDFILE  ]]; then             /bin/rm -f  $PIDFILE          fi    else        echo  "ERROR:  no chatterserver running "        # todo we  just think this is not essential         # Do NOT setting up log here         exit 1    fi}restart ()  {    echo  "info:  Restarting chatterserver "    stop    # those lines  will remove in next release    if [[ $ (netstat - Anop 2>/dev/null | grep  $SERVICEPORT  | grep listen)  ]]; then         echo  "warnning: port  $SERVICEPORT  is in using, must waiting"         sleep 5        if  [[ $ (netstat -anop 2>/dev/null | grep  $SERVICEPORT  | grep  listen)  ]]; then            echo   "warnning : port  $SERVICEPORT  is still in using, must  Waiting "            sleep 2         fi    fi    # -those lines  will remove in next release    # do not using  sleep any seconds&Nbsp;here with stop ()  function used    start}case $1 in     status)         status         ;;     start)         start         ;;     stop)         stop         ;;     restart)         restart         ;;     help|*)         echo  "usage: $0  {status|start|stop|restart|help} with $0 itself "         echo  "Usage: service chatter {status|start|stop|restart|help} with service "        exit 1        ;; esac# replace  "Exit 0"  with  ":" #exit  0:

--end--

This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1673822

Linux Shell's Chatterserver service control script

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.