-   Linux - Newbie (
-   -   java program as daemon (

dsubbarao 01-19-2007 12:50 AM

java program as daemon
Hi All,

I want to run one java program as a daemon on linux.
(That java program is the 3rd party one so i can not modify the source. I have created a jar file with manifest file specifying the main now i am able to run the program form jar with the command java -jar ex.jar.Now i want to make it a daemon so that i can stop and start and restart)

Can any body tell me what is the procedure to install the java program as daemon .so that i can restart it also?

Thanks in Advance

jlliagre 01-19-2007 03:10 AM

What kind of program is yours ?

dsubbarao 01-20-2007 03:05 AM

It is part of my application.It doesn't have anything to do with any ports and all.....It is just pure normal java program which must be executing after it has been installed.(When it starts it searches for any changes for rules those are made by application and keeps log of it.)

Thanks in Advance

jlliagre 01-20-2007 04:33 AM

If your program doesn't make use of its standard input nor use graphics (AWT / Swing), then you can launch it as a service.

The way to do it depends on what your distro is. For examples, the System V way is creating a stop/start script in /etc/init.d then create lints to it in the appropriate run levels directories (eg. /etc/rc2.d/S99myJavaDaemon).

dsubbarao 01-21-2007 04:51 AM

Thanks for the reply.

I am using redhat.I have written the rcscripts for my java program.
I am able to start the service .But I am not able to stop the service.How to stop the service ?? What i had read is i have to create the pid in /var/run directory and we have to give command "kill pid" but i am not able to stop the service ..
I don't have special code to stop the java program.

Can u tell me indetail how to create the pid or the way to stop the service..

I am giving u my rc script:

# chkconfig: 2345 16 84
# description: RulesEngine
# processname: re
# Source function library.
. /etc/rc.d/init.d/functions



start() {
echo -n $"Starting RE: "
cd root
source .bshrc

local filename="$pidFile"
touch $filename || return 1

chmod g+w $filename || return 1

cd /root/workrpm/usr/share/GRE/
java -jar relight.jar
return $RETVAL

stop() {
local pid="$(<$pidFile)"
echo $pid
kill $pid || return 1

sleep 2

# See how we were called.
case "$1" in
echo "Usage: $0 {start|stop|restart}"
exit 1

exit $RETVAL

Thanks in Advance.

jlliagre 01-21-2007 06:03 AM

Try something like

pkill -f "java -jar relight.jar"

dsubbarao 01-21-2007 11:29 PM

HI jlliagre
Hi jlliagre

Thanks very much..It did work like a charm..

You have made my day...

Thanks once again.

dsubbarao 01-22-2007 12:16 AM


But without creating the pid file is there any way to find out the service is started or not???

Thanks in Advance?

jlliagre 01-22-2007 12:49 AM


pgrep -f "java -jar relight.jar"

creativemadhan 03-01-2007 11:17 PM

I am new to scripts, so can u guys tell me whether a jar file and script file without any pid file is enough for creating a service...:confused:

All times are GMT -5. The time now is 10:53 PM.