#!/bin/bash # # chkconfig: 345 99 05 # # description: Java deamon script for davmail (http://davmail.sourceforge.net/) write by qk4l@tem4uk.ru 2011 # # Derived from - # Home page: http://www.source-code.biz # License: GNU/LGPL (http://www.gnu.org/licenses/lgpl.html) # Copyright 2006 Christian d'Heureuse, Inventec Informatik AG, Switzerland. # # History: # 2010-09-21 Josh Davis: Changed 'sudo' to 'su', fix some typos, removed unused variables # 2009-03-04 Josh Davis: Ubuntu/Redhat version. # 2006-06-27 Christian d'Heureuse: Script created. # 2006-07-02 chdh: Minor improvements. # 2006-07-10 chdh: Changes for SUSE 10.0. # Set this to your Java installation serviceNameLo="davmail" # service name with the first letter in lowercase serviceName="Davmail" # service name serviceUser="davmail" # OS user name for the service serviceGroup="root" # OS group name for the service applDir="/usr/share/$serviceNameLo/lib" # home directory of the service application serviceUserHome="/etc/$serviceNameLo" # home directory of the service user serviceLogFile="/var/log/$serviceNameLo.log" # log file for StdOut/StdErr maxShutdownTime=15 # maximum number of seconds to wait for the daemon to terminate normally pidFile="/var/run/$serviceNameLo.pid" # name of PID file (PID = process ID number) javaCommand="java" # name of the Java launcher without the path javaExe="/usr/bin/$javaCommand -Xmx512M" # file name of the Java application launcher executable javaCommandLineKeyword="davmail.jar" # a keyword that occurs on the commandline, used to detect an already running service process and to distinguish it from others DAEMON_ARGS="/etc/davmail/davmail.properties" # Makes the file $1 writable by the group $serviceGroup. function makeFileWritable { local filename="$1" touch $filename || return 1 chown $serviceUser:$serviceGroup $filename || return 1 #chgrp $serviceGroup $filename || return 1 chmod g+w $filename || return 1 return 0; } # Returns 0 if the process with PID $1 is running. function checkProcessIsRunning { local pid="$1" if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi if [ ! -e /proc/$pid ]; then return 1; fi return 0; } # Returns 0 if the process with PID $1 is our Java service process. function checkProcessIsOurService { local pid="$1" if [ "$(ps -p $pid --no-headers -o comm)" != "$javaCommand" ]; then return 1; fi grep -q --binary -F "$javaCommandLineKeyword" /proc/$pid/cmdline if [ $? -ne 0 ]; then return 1; fi return 0; } # Returns 0 when the service is running and sets the variable $pid to the PID. function getServicePID { if [ ! -f $pidFile ]; then return 1; fi pid="$(<$pidFile)" checkProcessIsRunning $pid || return 1 checkProcessIsOurService $pid || return 1 return 0; } function startServiceProcess { cd $applDir || return 1 rm -f $pidFile makeFileWritable $pidFile || return 1 makeFileWritable $serviceLogFile || return 1 for i in $applDir/*; do export CLASSPATH=$CLASSPATH:$i; done cmd="nohup $javaExe -cp /usr/share/davmail/davmail.jar:$CLASSPATH davmail.DavGateway $DAEMON_ARGS >>$serviceLogFile 2>&1 & echo \$! >$pidFile" #echo $cmd su -m $serviceUser -s $SHELL -c "$cmd" || return 1 sleep 0.1 pid="$(<$pidFile)" if checkProcessIsRunning $pid; then :; else echo -ne "\n$serviceName start failed, see logfile." return 1 fi return 0; } function stopServiceProcess { kill $pid || return 1 for ((i=0; i