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;
# description: LinuxAgent
scriptFile=$(readlink -fn $(type -p $0))
scriptDir=$(dirname $scriptFile)
javaArgs="-jar /LinuxAgent/LinuxAgent.jar"
javaCommandLine="$javaCommand $javaArgs"

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
if checkProcessIsRunning $pid; then :; else
echo "$serviceName service can not be started, log file $serviceLogFile ."
return 1
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
sleep 0.1
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
sleep 0.1
echo "Error: $serviceName can not stop in $maxShutdownTime+$killWaitTime "
return 1; }

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

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

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

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

function main {
case "$1" in
stopService && startService
echo "Usage: $0 {start|stop|restart|status}"
exit 1
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


Popular posts from this blog

Creating Multiple VLANs over Bonding Interfaces with Proper Routing on a Centos Linux Host

In this post, I am going to explain configuring multiple VLANs on a bond interface. First and foremost, I would like to describe the environment and give details of the infrastructure. The server has 4 Ethernet links to a layer 3 switch with names: enp3s0f0, enp3s0f1, enp4s0f0, enp4s0f1 There are two bond interfaces both configured as active-backup bond0, bond1 enp4s0f0 and enp4s0f1 interfaces are bonded as bond0. Bond0 is for making ssh connections and management only so corresponding switch ports are not configured in trunk mode. enp3s0f0 and enp3s0f1 interfaces are bonded as bond1. Bond1 is for data and corresponding switch ports are configured in trunk mode. Bond0 is the default gateway for the server and has IP address Bond1 has three subinterfaces with VLAN 4, 36, 41. IP addresses are,, respectively. Proper communication with other servers on the network we should use routing tables. There are three

PowerShell Script for Switching Between Multiple Windows

Windows PowerShell has strong capabilities. I have a separate computer with a big lcd screen in which I am watching regularly some web based monitoring applications. So I need those application windows switch between on a timely basis. Then I wrote this simple powershell script to achieve this. You can change it according to your needs.