bin.runner Maven / Gradle / Ivy
#!/bin/bash
serviceUser=`id -un`
serviceGroup=`id -gn`
userHome=`eval echo ~$serviceUser`
applDir="$scriptDir"
serviceUserHome="$applDir"
maxShutdownTime=15
pidFile="$WD/work/$serviceNameLo.pid"
javaCommand="java"
javaExe="$JAVA_HOME/bin/$javaCommand"
commandLine="$javaExe $args"
mkdir -p $WD/work/repo
mkdir -p $WD/log
# Makes the file $1 writable by the group $serviceGroup.
function makeFileWritable {
local filename="$1"
touch $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 ps -p "$pid" > /dev/null 2>&1; then return 0; else return 1; fi }
# Returns 0 if the process with PID $1 is our Java service process.
function checkProcessIsOurService {
local pid="$1"
if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
if [ "$(pgrep -f "$serviceNameLo") == *$pid*" ]; then return 0; else return 1; fi }
# 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
cmd="nohup $commandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
# Don't forget to add -H so the HOME environment variable will be set correctly.
$SHELL -c "$cmd" || return 1
sleep 2
pid="$(<$pidFile)"
if checkProcessIsRunning $pid; then :; else
echo -ne "\n$serviceName start failed, see logfile $serviceLogFile."
return 1
fi
return 0; }
function stopServiceProcess {
kill $pid || return 1
for ((i=0; i