Advertisement
Guest User

init script for DAVmail

a guest
Jun 2nd, 2011
1,550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.98 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # chkconfig: 345 99 05
  4. # # description: Java deamon script for davmail (http://davmail.sourceforge.net/) write by qk4l@tem4uk.ru 2011
  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. #
  11. # History:
  12. # 2010-09-21 Josh Davis: Changed 'sudo' to 'su', fix some typos, removed unused variables
  13. # 2009-03-04 Josh Davis: Ubuntu/Redhat version.
  14. # 2006-06-27 Christian d'Heureuse: Script created.
  15. # 2006-07-02 chdh: Minor improvements.
  16. # 2006-07-10 chdh: Changes for SUSE 10.0.
  17.  
  18.  
  19. # Set this to your Java installation
  20.  
  21. serviceNameLo="davmail"                                  # service name with the first letter in lowercase
  22. serviceName="Davmail"                                    # service name
  23. serviceUser="davmail"                                      # OS user name for the service
  24. serviceGroup="root"                                    # OS group name for the service
  25. applDir="/usr/share/$serviceNameLo/lib"                          # home directory of the service application
  26. serviceUserHome="/etc/$serviceNameLo"                       # home directory of the service user
  27. serviceLogFile="/var/log/$serviceNameLo.log"               # log file for StdOut/StdErr
  28. maxShutdownTime=15                                         # maximum number of seconds to wait for the daemon to terminate normally
  29. pidFile="/var/run/$serviceNameLo.pid"                      # name of PID file (PID = process ID number)
  30. javaCommand="java"                                         # name of the Java launcher without the path
  31. javaExe="/usr/bin/$javaCommand -Xmx512M"                      # file name of the Java application launcher executable
  32. javaCommandLineKeyword="davmail.jar"                     # a keyword that occurs on the commandline, used to detect an already running service process and to distinguish it from others
  33. DAEMON_ARGS="/etc/davmail/davmail.properties"
  34.  
  35. # Makes the file $1 writable by the group $serviceGroup.
  36. function makeFileWritable {
  37.    local filename="$1"
  38.    touch $filename || return 1
  39.    chown $serviceUser:$serviceGroup $filename || return 1
  40.    #chgrp $serviceGroup $filename || return 1
  41.    chmod g+w $filename || return 1
  42.    return 0; }
  43.  
  44. # Returns 0 if the process with PID $1 is running.
  45. function checkProcessIsRunning {
  46.    local pid="$1"
  47.    if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
  48.    if [ ! -e /proc/$pid ]; then return 1; fi
  49.    return 0; }
  50.  
  51. # Returns 0 if the process with PID $1 is our Java service process.
  52. function checkProcessIsOurService {
  53.    local pid="$1"
  54.    if [ "$(ps -p $pid --no-headers -o comm)" != "$javaCommand" ]; then return 1; fi
  55.    grep -q --binary -F "$javaCommandLineKeyword" /proc/$pid/cmdline
  56.    if [ $? -ne 0 ]; then return 1; fi
  57.    return 0; }
  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. function startServiceProcess {
  68.    cd $applDir || return 1
  69.    rm -f $pidFile
  70.    makeFileWritable $pidFile || return 1
  71.    makeFileWritable $serviceLogFile || return 1
  72.    for i in $applDir/*; do export CLASSPATH=$CLASSPATH:$i; done
  73.    cmd="nohup $javaExe -cp /usr/share/davmail/davmail.jar:$CLASSPATH davmail.DavGateway $DAEMON_ARGS >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
  74.    #echo $cmd
  75.    su -m $serviceUser -s $SHELL -c "$cmd" || return 1
  76.    sleep 0.1
  77.    pid="$(<$pidFile)"
  78.    if checkProcessIsRunning $pid; then :; else
  79.       echo -ne "\n$serviceName start failed, see logfile."
  80.       return 1
  81.    fi
  82.    return 0; }
  83.  
  84. function stopServiceProcess {
  85.    kill $pid || return 1
  86.    for ((i=0; i<maxShutdownTime*10; i++)); do
  87.       checkProcessIsRunning $pid
  88.       if [ $? -ne 0 ]; then
  89.          rm -f $pidFile
  90.          return 0
  91.          fi
  92.       sleep 0.1
  93.       done
  94.    echo -e "\n$serviceName did not terminate within $maxShutdownTime seconds, sending SIGKILL..."
  95.    kill -s KILL $pid || return 1
  96.    local killWaitTime=15
  97.    for ((i=0; i<killWaitTime*10; i++)); do
  98.       checkProcessIsRunning $pid
  99.       if [ $? -ne 0 ]; then
  100.          rm -f $pidFile
  101.          return 0
  102.          fi
  103.       sleep 0.1
  104.       done
  105.    echo "Error: $serviceName could not be stopped within $maxShutdownTime+$killWaitTime seconds!"
  106.    return 1; }
  107.  
  108. function startService {
  109.    getServicePID
  110.    if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; return 0; fi
  111.    echo -n "Starting $serviceName   "
  112.    startServiceProcess
  113.    if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
  114.    echo "started PID=$pid"
  115.    RETVAL=0
  116.    return 0; }
  117.  
  118. function stopService {
  119.    getServicePID
  120.    if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
  121.    echo -n "Stopping $serviceName   "
  122.    stopServiceProcess
  123.    if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
  124.    echo "stopped PID=$pid"
  125.    RETVAL=0
  126.    return 0; }
  127.  
  128. function checkServiceStatus {
  129.    echo -n "Checking for $serviceName:   "
  130.    if getServicePID; then
  131.     echo "running PID=$pid"
  132.     RETVAL=0
  133.    else
  134.     echo "stopped"
  135.     RETVAL=3
  136.    fi
  137.    return 0; }
  138.  
  139. function main {
  140.    RETVAL=0
  141.    case "$1" in
  142.       start)                                               # starts the Java program as a Linux service
  143.          startService
  144.          ;;
  145.       stop)                                                # stops the Java program service
  146.          stopService
  147.          ;;
  148.       restart)                                             # stops and restarts the service
  149.          stopService && startService
  150.          ;;
  151.       status)                                              # displays the service status
  152.          checkServiceStatus
  153.          ;;
  154.       *)
  155.          echo "Usage: $0 {start|stop|restart|status}"
  156.          exit 1
  157.          ;;
  158.       esac
  159.    exit $RETVAL
  160. }
  161.  
  162. main $1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement