LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   java program as daemon (https://www.linuxquestions.org/questions/linux-newbie-8/java-program-as-daemon-520770/)

dsubbarao 01-19-2007 01: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 class.so 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 04:10 AM

What kind of program is yours ?

dsubbarao 01-20-2007 04: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 05: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 05: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:

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

prog=re
pidFile="/var/run/re/re.pid"

RETVAL=0


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

}
restart(){
stop
sleep 2
start
}

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

exit $RETVAL


Thanks in Advance.

jlliagre 01-21-2007 07:03 AM

Try something like
Code:

pkill -f "java -jar relight.jar"

dsubbarao 01-22-2007 12:29 AM

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 01:16 AM

HI

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 01:49 AM

Sure:
Code:

pgrep -f "java -jar relight.jar"

creativemadhan 03-02-2007 12:17 AM

Hi,
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 04:16 PM.