Skip to main content

Running java program as a linux service

Recently, I was wondering about how the programs are running as a service in Linux. After a little googling,  i learned it is very simple. All we have to do is just write a script that starts, stops and restarts the program and register this script as a Linux service. Let me tell the scenario.

I have just written a simple java program that connects a database and sends cpu, memory and disk I/O information for monitoring the server performance. It is running on the Fedora Linux operating system. Java version 1.6.0_03. Java program is located under /LinuxAgent directory.

Thanks for google i found this script and changed the necessary parameters (Program path, java launcher, service name etc.)

Here is the contents of the script;
#!/bin/sh
# description: LinuxAgent
scriptFile=$(readlink -fn $(type -p $0))
scriptDir=$(dirname $scriptFile)
applDir="$scriptDir"
serviceName="LinuxAgent"
serviceNameLo="LinuxAgent"
serviceGroup="root"
pidFile="/var/run/$serviceNameLo.pid"
serviceLogFile="/var/log/$serviceNameLo.log"
javaCommand="java"
javaArgs="-jar /LinuxAgent/LinuxAgent.jar"
javaCommandLine="$javaCommand $javaArgs"
javaCommandLineKeyword="LinuxAgent"
maxShutdownTime=15

function makeFileWritable {
local filename="$1"
touch $filename || return 1
chgrp $serviceGroup $filename || return 1
chmod g+w $filename || return 1
return 0; }

function checkProcessIsRunning {
local pid="$1"
if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
if [ ! -e /proc/$pid ]; then return 1; fi
return 0; }

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; }

function startServiceProcess {
cd $applDir || return 1
rm -f $pidFile
makeFileWritable $pidFile || return 1
makeFileWritable $serviceLogFile || return 1
cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo \$! >$pidFile"
#sudo -u $serviceUser $SHELL -c "$cmd" || return 1
$SHELL -c "$cmd" || return 1
sleep 0.1
pid="$(<$pidFile)"
if checkProcessIsRunning $pid; then :; else
echo "$serviceName service can not be started, log file $serviceLogFile ."
return 1
fi
return 0; }

function stopServiceProcess {
kill $pid || return 1
for ((i=0; i<maxShutdownTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo "$serviceName can not stop in $maxShutdownTime , sending SIGKILL"
kill -s KILL $pid || return 1
local killWaitTime=15
for ((i=0; i<killWaitTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo "Error: $serviceName can not stop in $maxShutdownTime+$killWaitTime "
return 1; }

function getServicePID {
if [ ! -f $pidFile ]; then return 1; fi
pid="$(<$pidFile)"
checkProcessIsRunning $pid || return 1
checkProcessIsOurService $pid || return 1
return 0; }

function startService {
getServicePID
if [ $? -eq 0 ]; then echo "$serviceName is already running"; return 0; fi
echo "$serviceName is starting"
startServiceProcess
if [ $? -ne 0 ]; then return 1; fi
return 0; }

function stopService {
getServicePID
if [ $? -ne 0 ]; then echo "$serviceName is not running"; return 0; fi
echo "$serviceName is stopping"
stopServiceProcess
if [ $? -ne 0 ]; then return 1; fi
return 0; }

function checkServiceStatus {
echo -n "$serviceName is controlling: "
if getServicePID; then
echo "LinuxAgent is running"
else
echo "LinuxAgent is not running"
fi
return 0; }

function main {
case "$1" in
start)
startService
;;
stop)
stopService
;;
restart)
stopService && startService
;;
status)
checkServiceStatus
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
}
main $1

Script must be saved under the /etc/inid.d directory and execute permission must be given;
$ chmod 755 /etc/init.d/LinuxAgent
Chkconfig utility registers the service;
$ chkconfig –add LinuxAgent
$ chkconfig LinuxAgent –level 12345 on
Now service is created and we start and stop service with these commands;
$ service LinuxAgent start
$ service LinuxAgent stop
To learn the status of the service;
$ service LinuxAgent status

Comments

Popular posts from this blog