Advertisement
gusto2

Java daemon (service) script

Nov 15th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.69 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # chkconfig: 345 99 05
  4. # description: A test service
  5. #
  6. # Derived from -
  7. # Home page: http://www.source-code.biz
  8. # License:   GNU/LGPL (http://www.gnu.org/licenses/lgpl.html)
  9. # Copyright 2006 Christian d'Heureuse, Inventec Informatik AG, Switzerland.
  10. # Copyright 2013 Gabriel Vince, Apogado, Belgium
  11.  
  12. set -x
  13.  
  14. if [ -z "$JAVA_HOME" -o "$JAVA_HOME" == " " ]; then
  15.        JAVA_HOME=/usr/java/latest
  16.        export JAVA_HOME
  17. fi
  18.  
  19. serviceName="program.sh" # service name
  20. serviceUser="gabriel" # OS user name for the service
  21. serviceGroup="users" # OS group name for the service
  22. applDir="/home/gabriel/test/service" # home directory of the service application
  23. serviceUserHome="/home/gabriel"  # home directory of the service user
  24. serviceLogFile="/home/gabriel/test/service/$serviceName.log" # log file for StdO
  25. ut/StdErr
  26. maxShutdownTime=15 # maximum number of seconds to wait for the daemon to termina
  27. te normally
  28. pidFile="/var/run/$serviceName.pid" # name of PID file (PID = process ID number)
  29. cmd="/home/gabriel/test/service/program.sh" # name of the Java launcher without
  30. the path
  31. appname="program.sh" #program name to look for between running processes
  32.  
  33. # Makes the file $1 writable by the group $serviceGroup.
  34. function makeFileWritable {
  35.    local filename="$1"
  36.    touch $filename || return 1
  37.    chgrp $serviceGroup $filename || return 1
  38.    chmod g+w $filename || return 1
  39. return 0;
  40. }
  41.  
  42. # Returns 0 if the process with PID $1 is running.
  43. function checkProcessIsRunning {
  44.    local pid="$1"
  45.    if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
  46.    if [ ! -e /proc/$pid ]; then return 1; fi
  47.    return 0;
  48. }
  49.  
  50. # Returns 0 if the process with PID $1 is our Java service process.
  51. function checkProcessIsOurService {
  52.    local pid="$1"
  53.    if [ "$(ps -p $pid --no-headers -o comm)" != "$appname" ]; then return 1; fi
  54.    grep -q --binary -F "$appname" /proc/$pid/cmdline
  55.    if [ $? -ne 0 ]; then return 1; fi
  56.    return 0;
  57. }
  58.  
  59. # Returns 0 when the service is running and sets the variable $pid to the PID.
  60. function getServicePID {
  61.    if [ ! -f $pidFile ]; then return 1; fi
  62.    pid="$(<$pidFile)"
  63.    checkProcessIsRunning $pid || return 1
  64.    checkProcessIsOurService $pid || return 1
  65.    return 0;
  66. }
  67.  
  68. function startServiceProcess {
  69.    cd $applDir || return 1
  70.    rm -f $pidFile
  71.    makeFileWritable $pidFile || return 1
  72.    makeFileWritable $serviceLogFile || return 1
  73.    cmd="nohup $cmd >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
  74.    su -m $serviceUser -s $SHELL -c "$cmd" || return 1
  75.    sleep 0.1
  76.    pid="$(<$pidFile)"
  77.    if checkProcessIsRunning $pid; then :; else
  78.      echo -ne "\n$serviceName start failed, see logfile."
  79.      return 1
  80.    fi
  81.    return 0;
  82. }
  83.  
  84. function stopServiceProcess {
  85.    kill $pid || return 1
  86.    for ((i=0; i<maxShutdownTime; i++)); do
  87.       checkProcessIsRunning $pid
  88.       if [ $? -ne 0 ]; then
  89.           rm -f $pidFile
  90.           return 0
  91.      fi
  92.      sleep 1
  93.   done
  94.  echo -e "\n$serviceName did not terminate within $maxShutdownTime seconds, send
  95. ing SIGKILL..."
  96.   kill -s KILL $pid || return 1
  97.  
  98.   local killWaitTime=15
  99.  for ((i=0; i<killWaitTime; i++)); do
  100.      checkProcessIsRunning $pid
  101.      if [ $? -ne 0 ]; then
  102.          rm -f $pidFile
  103.          return 0
  104.      fi
  105.      sleep 1
  106.      done
  107.      echo "Error: $serviceName could not be stopped within $maxShutdownTime+$kil
  108. lWaitTime seconds!"
  109.      return 1;
  110. }
  111.  
  112. function startService {
  113.    getServicePID
  114.    if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; re
  115. turn 0; fi
  116.    echo -n "Starting $serviceName   "
  117.    startServiceProcess
  118.    if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
  119.    echo "started PID=$pid"
  120.    RETVAL=0
  121.    return 0;
  122. }
  123.  
  124. function stopService {
  125.    getServicePID
  126.    if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo "
  127. "; return 0; fi
  128.    echo -n "Stopping $serviceName   "
  129.    stopServiceProcess
  130.    if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
  131.        echo "stopped PID=$pid"
  132.        RETVAL=0
  133.    return 0;
  134. }
  135.  
  136. function checkServiceStatus {
  137.    echo -n "Checking for $serviceName:   "
  138.    if getServicePID; then
  139.       echo "running PID=$pid"
  140.       RETVAL=0
  141.    else
  142.       echo "stopped"
  143.       RETVAL=3
  144.    fi
  145.    return 0;
  146. }
  147.  
  148.  
  149. function main {
  150.    RETVAL=0
  151.    case "$1" in
  152.    start) # starts the Java program as a Linux service
  153.       startService
  154.       ;;
  155.    stop)  # stops the Java program service
  156.      stopService
  157.      ;;
  158.    restart) # stops and restarts the service
  159.     stopService && startService
  160.     ;;
  161.   status) # displays the service status
  162.     checkServiceStatus
  163.     ;;
  164.   *)
  165.       echo "Usage: $0 {start|stop|restart|status}"
  166.       exit 1
  167.       ;;
  168. esac
  169. exit $RETVAL
  170. }
  171.  
  172. main $1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement