Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-30-2009, 12:10 PM   #1
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


# 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"

# 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

### main logic ###
case "$1" in
echo $"Usage: $0 {start|stop|restart}"
exit 1

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,

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

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

Old 10-14-2011, 04:10 AM   #3
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:
// 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:
# 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 04:27 AM.


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 12:40 PM
run a java application (server) as service fireball003 Linux - Newbie 8 04-08-2009 07:05 AM
how do i execute a jar (java application) in linux amolgupta Linux - Software 2 07-20-2006 06:55 AM
Fedora Core 1 Associating .jar files with java -jar command pymehta Fedora 0 01-13-2005 06:26 AM
How To Program Daemon(Service) Application wlmapp3 Programming 6 07-23-2003 04:58 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:32 PM.

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