LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-08-2010, 06:38 AM   #1
deostroll
Member
 
Registered: Aug 2007
Location: Bangalore
Distribution: fedora
Posts: 112

Rep: Reputation: 15
write a shell script so that it can run as service/daemon


Hi,

Am not sure of the best way to go about writing a script. All I need at the moment is to start a java http server program at startup. I have the following script for this...

Code:
# /var/www/html/languagetool/languagetool.sh

if [ $PWD != '/var/www/html/languagetool' ]
then
	cd /var/www/html/languagetool	
fi

echo "Starting languagetool in " "${PWD}" 

java -cp jaminid.jar:LanguageTool.jar de.danielnaber.languagetool.server.HTTPServer
I have the chkconfig ready script in the following pattern:
Code:
case "$1" in
  start)
        echo -n "Starting languagetool service"
        #To run it as root:
        /var/www/html/languagetool/languagetool.sh
        ;;
  stop)
        echo -n "Stopping languagetool"
        # wondering how to stop this...
        ;;

  *)
        echo "Usage: /sbin/service languagetool {start|stop}"
        exit 1
esac

exit 0
How should I write languagetool.sh so as to be able to stop the script execution?
 
Old 03-08-2010, 07:05 AM   #2
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
Code:
killall -9 java
Your script isn't running full time (is it ?), it just starts a java process.

If you have other java instances that you don't want to kill at the same time, then you will have to grep the process list for the one you want.

Run the script, start the java http program and then look in your process lists for the program.
When you have identified the program, you could run something like this to stop that process :
Code:
kill `ps -ef | grep my_java_program | grep -v grep | awk '{ print $2 }'`
replace my_java_program with the name that appears in the ps output for your program.

You could try using something like this for starting too :
http://jlorenzen.blogspot.com/2007/1...p-scripts.html

Last edited by smoker; 03-08-2010 at 07:12 AM.
 
1 members found this post helpful.
Old 03-08-2010, 07:28 AM   #3
deostroll
Member
 
Registered: Aug 2007
Location: Bangalore
Distribution: fedora
Posts: 112

Original Poster
Rep: Reputation: 15
hi anyway to execute languagetool.sh in an asynchronous manner. When we start at service from the shell it blocks, and does not return to the prompt.
 
Old 03-08-2010, 07:32 AM   #4
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
Is that a question ?

Check my link again, it tells you how to run the command in the background so your script can finish.
 
Old 03-08-2010, 07:53 AM   #5
deostroll
Member
 
Registered: Aug 2007
Location: Bangalore
Distribution: fedora
Posts: 112

Original Poster
Rep: Reputation: 15
I've written my script file as follows:
Code:
# /var/www/html/languagetool/languagetool.sh

if [ $PWD != '/var/www/html/languagetool' ]
then
	cd /var/www/html/languagetool	
fi

echo "Starting languagetool in " "${PWD}" 

nohup nice 'java -cp jaminid.jar:LanguageTool.jar de.danielnaber.languagetool.server.HTTPServer'
but it doesn't start the java program...
PS: its the java program thats blocking...not the script to be precise...
 
Old 03-08-2010, 08:04 AM   #6
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
You missed off the & at the end of the line.

Look at the example again.
 
Old 03-08-2010, 08:15 AM   #7
deostroll
Member
 
Registered: Aug 2007
Location: Bangalore
Distribution: fedora
Posts: 112

Original Poster
Rep: Reputation: 15
something wrong...the output file says
Code:
nice: java -cp jaminid.jar:LanguageTool.jar \
de.danielnaber.languagetool.server.HTTPServer: No such file or directory
 
Old 03-08-2010, 08:56 AM   #8
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 248Reputation: 248Reputation: 248
1) You don't need nice
2) why did you put single quotes around the command ? You didn't need them before.
 
1 members found this post helpful.
Old 03-08-2010, 09:17 AM   #9
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,011

Rep: Reputation: 110Reputation: 110
Do not use kill -9 in scripts, ever. It tells a process to "just die right now" without giving any opportunity for cleanup. It's meant only when you've got a runaway process that you need dead and you'll take responsibility for the fallout.

http://mywiki.wooledge.org/ProcessManagement
 
Old 03-08-2010, 09:44 AM   #10
deostroll
Member
 
Registered: Aug 2007
Location: Bangalore
Distribution: fedora
Posts: 112

Original Poster
Rep: Reputation: 15
@smoker its to demarcate that that is the command I am trying to execute. Actually thought that -cp (which is part of the java command structure) will be confused with nohup...
 
Old 03-08-2010, 10:03 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,531
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by deostroll View Post
@smoker its to demarcate that that is the command I am trying to execute. Actually thought that -cp (which is part of the java command structure) will be confused with nohup...
Adding the quotes made nohup look for a file called 'java -cp jaminid.jar:LanguageTool.jar de.danielnaber.languagetool.server.HTTPServer' in all the directories listed in $PATH. Not surprisingly it didn't find it.

nohup would not be confused by the -cp. See the nohup man page for why not.
 
  


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
How to write a daemon Linux service to a Java application which execute a .jar haseit Linux - Newbie 2 10-14-2011 03:10 AM
How to run JBoss 5 as a Service / Daemon on Fedora 8? socal_javaguy Linux - Software 1 07-31-2009 11:30 AM
Enable daemon to run with service daemon start,etc baddah Programming 6 12-02-2007 05:51 PM
Configuring Shell Script as service/daemon nkanthikiran Red Hat 2 11-07-2006 05:50 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration