LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-30-2009, 11:10 AM   #1
haseit
LQ Newbie
 
Registered: Jan 2009
Location: Colombo / Sri Lanka
Posts: 14

Rep: Reputation: 0
How to write a daemon Linux service to a Java application which execute a .jar


Hello All,

I've some question related to How to properly set up a Linux service to a Java application which execute a .jar. My requirement is to is to start my application as a Service when boot Linux and it should be able to stop , start, restart at any time and it should be able to stop when the system is shutting down.

My question is basically focus on CentOs, Red Hat Linux 9, Ubuntu.

I wrote a shell script which use as the service execution point and i've place the she script in /etc/init.d path in CentOs, Red Hat Linux 9 and Ubuntu.

but when the system is initializing the Java application doesn't execute i had to start it manually but my requirement didn't fulfill.

Below define my shell script witch i use to execute as a Service

Quote:

#!/bin/bash
#
# chkconfig:
# description: Matrix Agent
#

# Get function from functions library
. /etc/init.d/functions


# Start the service Matrix
start() {
initlog -c "echo -n Starting Matrix Agent: "
cd /usr/matrix
java -jar /usr/matrix/MatrixAgent.jar &
### Create the lock file ###
touch /var/lock/subsys/Matrix
success $"Matrix Agent startup"
echo
}

# Restart the service Matrix
stop() {
initlog -c "echo -n Stopping Matrix Agent: "
kill proc Matrix
### Now, delete the lock file ###
rm -f /var/lock/subsys/Matrix
echo
}

### main logic ###
case "$1" in
start)
start
;;
stop)
stop
;;
restart|reload)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac

exit $?




Please look at my matter and help me to make this application execute successfully.

Please guide me step by step where to put and how to make it work.

Thanks in advance,

Hasantha
 
Old 03-30-2009, 10:51 PM   #2
haseit
LQ Newbie
 
Registered: Jan 2009
Location: Colombo / Sri Lanka
Posts: 14

Original Poster
Rep: Reputation: 0
Please Help me on this matter

Regards,
Hasantha
 
Old 10-14-2011, 03:10 AM   #3
Weeman
LQ Newbie
 
Registered: Oct 2011
Posts: 1

Rep: Reputation: Disabled
Thumbs up

zzup Hasantha,

At this moment you've already solved this problem, I guess. But I had a similar problem which almost made me bold and I finally managed to fix it.
And I have a feeling that some people would appreciate some of this knowledge.

To make your service runnable at boot you have to run the following commands:
Quote:
// add your script to chkconfig which updates and queries runlevel information for system services
chkconfig --add <my_script>

// see if your service was added to chkconfig
chkconfig --list

// In this example I'm selecting runlevels 2, 3 and 4 for my script. To see more information about runlevels, google chkconfig (I'm not allowed to post links for some reason because my account is new... )
// don't forget to enable the script by saying "on" at the end of the command
chkconfig --level 234 <my_script> on
Another solution is to set chkconfig settings directly into your script:
Quote:
#!/bin/bash
#
# chkconfig: 234 10 60
# description: My awesome script
#
This part of the init script is what chkconfig looks for, and, in this instance, it tells chkconfig that the service should start at run levels 2, 3 and 4,
it should be turned off for all other run levels (chkconfig's default assumption) and that it should have a start priority of 10 and a stop priority of 60.
The priority numbers that you include in your script are relative and shouldn't generally be an issue. The defaults are usually 20 and 80.
You can also add a "#description:" line (directly after the required chkconfig line) to your script if you want to be able to see that in chkconfig output, but it's not necessary.

Last edited by Weeman; 10-14-2011 at 03:27 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
KDE jar MIME is application/zip and not application/java-archive in Ubuntu Intrepid? arobinson74 Linux - Software 2 09-18-2009 11:40 AM
run a java application (server) as service fireball003 Linux - Newbie 8 04-08-2009 06:05 AM
how do i execute a jar (java application) in linux amolgupta Linux - Software 2 07-20-2006 05:55 AM
Fedora Core 1 Associating .jar files with java -jar command pymehta Fedora 0 01-13-2005 05:26 AM
How To Program Daemon(Service) Application wlmapp3 Programming 6 07-23-2003 03:58 PM


All times are GMT -5. The time now is 01:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration