source: raptor-distribution/MSIs/raptor-ica/tags/v1.0.1/source/ica/bin/raptoricad @ 1711

Revision 1711, 7.5 KB checked in by rhyssmith, 6 years ago (diff)

Tagging raptor-ica v1.0.1-1 release of Raptor ICA MSI

  • Property svn:executable set to *
Line 
1# Adapted from SUSE Linux start/stop script for Java daemons.
2#
3# Home page: http://www.source-code.biz
4# License:   GNU/LGPL (http://www.gnu.org/licenses/lgpl.html)
5# Copyright 2006 Christian d'Heureuse, Inventec Informatik AG, Switzerland.
6#
7# History:
8# 2006-06-27 Christian d'Heureuse: Script created.
9# 2006-07-02 chdh: Minor improvements.
10# 2006-07-10 chdh: Changes for SUSE 10.0.
11# 2009-03-04 Josh Davis: Ubuntu/Redhat version.
12# 2010-09-21 Josh Davis: Changed 'sudo' to 'su', fix some typos, removed unused variables
13# 2011-05-12 Rhys Smith: Changed for RaptorICA
14
15### BEGIN INIT INFO
16# Provides:                   raptoricad
17# Required-Start:             $network $local_fs $remote_fs
18# Required-Stop:              $network $local_fs $remote_fs
19# Default-Start:              3 5
20# Default-Stop:               0 1 2 6
21# Short-Description: initscript to start/stop the Raptor ICA
22# Description:       initscript to start/stop the Raptor ICA
23### END INIT INFO
24
25if [ -z "$JAVA_HOME" ]; then                               # if JAVA_HOME is undefined
26   if [ -f /usr/share/java-utils/java-functions ]; then
27      . /usr/share/java-utils/java-functions; set_jvm      # JPackage standard method to set JAVA_HOME
28    elif [ -n "$(type -t setJava)" ]; then
29      . setJava ""; fi                                     # old SUSE method to set JAVA_HOME
30   if [ -z "$JAVA_HOME" ]; then echo Unable to set JAVA_HOME environment variable; exit 1; fi
31   fi
32
33serviceNameLo="raptoricad"                                 # service name with the first letter in lowercase
34serviceName="raptoricad"                                   # service name
35serviceUser="root"                                         # OS user name for the service
36serviceGroup="root"                                        # OS group name for the service
37
38applDir="/opt/raptor/ica"                                  # home directory of the service application
39serviceUserHome="$applDir"                                 # home directory of the service user
40
41scriptFile=$(readlink -fn $(type -p $0))                   # the absolute, dereferenced path of this script file
42scriptDir=$(dirname $scriptFile)                           # absolute path of the script directory
43
44serviceLogFile="/var/log/$serviceNameLo.log"               # log file for StdOut/StdErr
45maxShutdownTime=15                                         # maximum number of seconds to wait for the daemon to terminate normally
46
47pidFile="/var/run/$serviceNameLo.pid"                      # name of PID file (PID = process ID number)
48
49javaCommand="java"                                         # name of the Java launcher without the path
50javaExe="$JAVA_HOME/bin/$javaCommand"                      # file name of the Java application launcher executable
51javaArgs="-DconfigurationFiles=/opt/raptor/ica/conf/ -Xmx64M -jar raptor-ica.jar -server"
52                                                           # arguments for Java launcher
53javaCommandLine="$javaExe $javaArgs"                       # command line to start the Java service application
54javaCommandLineKeyword="raptor-ica.jar"                    # a keyword that occurs on the commandline, used to detect an already running service process and to distinguish it from others
55rcFileBaseName="rcraptoricad"                              # basename of the "rc" symlink file for this script
56rcFileName="/usr/local/sbin/rcraptoricad"                  # full path of the "rc" symlink file for this script
57etcInitDFile="/etc/init.d/raptoricad"                      # symlink to this script from /etc/init.d
58
59# Makes the file $1 writable by the group $serviceGroup.
60function makeFileWritable {
61   local filename="$1"
62   touch $filename || return 1
63   chgrp $serviceGroup $filename || return 1
64   chmod g+w $filename || return 1
65   return 0; }
66
67# Returns 0 if the process with PID $1 is running.
68function checkProcessIsRunning {
69   local pid="$1"
70   if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
71   if [ ! -e /proc/$pid ]; then return 1; fi
72   return 0; }
73
74# Returns 0 if the process with PID $1 is our Java service process.
75function checkProcessIsOurService {
76   local pid="$1"
77   if [ "$(ps -p $pid --no-headers -o comm)" != "$javaCommand" ]; then return 1; fi
78   grep -q --binary -F "$javaCommandLineKeyword" /proc/$pid/cmdline
79   if [ $? -ne 0 ]; then return 1; fi
80   return 0; }
81
82# Returns 0 when the service is running and sets the variable $pid to the PID.
83function getServicePID {
84   if [ ! -f $pidFile ]; then return 1; fi
85   pid="$(<$pidFile)"
86   checkProcessIsRunning $pid || return 1
87   checkProcessIsOurService $pid || return 1
88   return 0; }
89
90function startServiceProcess {
91   cd $applDir || return 1
92   rm -f $pidFile
93   makeFileWritable $pidFile || return 1
94   makeFileWritable $serviceLogFile || return 1
95   cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
96   su -m $serviceUser -s $SHELL -c "$cmd" || return 1
97   sleep 0.1
98   pid="$(<$pidFile)"
99   if checkProcessIsRunning $pid; then :; else
100      echo -ne "\n$serviceName start failed, see logfile."
101      return 1
102   fi
103   return 0; }
104
105function stopServiceProcess {
106   kill $pid || return 1
107   for ((i=0; i<maxShutdownTime*10; i++)); do
108      checkProcessIsRunning $pid
109      if [ $? -ne 0 ]; then
110         rm -f $pidFile
111         return 0
112         fi
113      sleep 0.1
114      done
115   echo -e "\n$serviceName did not terminate within $maxShutdownTime seconds, sending SIGKILL..."
116   kill -s KILL $pid || return 1
117   local killWaitTime=15
118   for ((i=0; i<killWaitTime*10; i++)); do
119      checkProcessIsRunning $pid
120      if [ $? -ne 0 ]; then
121         rm -f $pidFile
122         return 0
123         fi
124      sleep 0.1
125      done
126   echo "Error: $serviceName could not be stopped within $maxShutdownTime+$killWaitTime seconds!"
127   return 1; }
128
129function runInConsoleMode {
130   getServicePID
131   if [ $? -eq 0 ]; then echo "$serviceName is already running"; return 1; fi
132   cd $applDir || return 1
133   $javaCommandLine || return 1
134   if [ $? -eq 0 ]; then return 1; fi
135   return 0; }
136
137function startService {
138   getServicePID
139   if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; return 0; fi
140   echo -n "Starting $serviceName   "
141   startServiceProcess
142   if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
143   echo "started PID=$pid"
144   RETVAL=0
145   return 0; }
146
147function stopService {
148   getServicePID
149   if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
150   echo -n "Stopping $serviceName   "
151   stopServiceProcess
152   if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
153   echo "stopped PID=$pid"
154   RETVAL=0
155   return 0; }
156
157function checkServiceStatus {
158   echo -n "Checking for $serviceName:   "
159   if getServicePID; then
160        echo "running PID=$pid"
161        RETVAL=0
162   else
163        echo "stopped"
164        RETVAL=3
165   fi
166   return 0; }
167
168
169function main {
170   RETVAL=0
171   case "$1" in
172      start)                                               # starts the Java program as a Linux service
173         startService
174         ;;
175      stop)                                                # stops the Java program service
176         stopService
177         ;;
178      restart)                                             # stops and restarts the service
179         stopService && startService
180         ;;
181      console)                                             # runs in console mode
182        runInConsoleMode
183        ;;
184      status)                                              # displays the service status
185         checkServiceStatus
186         ;;
187      *)
188         echo "Usage: $0 {start|stop|restart|console|status}"
189         exit 1
190         ;;
191      esac
192   exit $RETVAL
193}
194
195main $1
196
Note: See TracBrowser for help on using the repository browser.