LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   write a shell script so that it can run as service/daemon (https://www.linuxquestions.org/questions/programming-9/write-a-shell-script-so-that-it-can-run-as-service-daemon-793943/)

deostroll 03-08-2010 06:38 AM

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?

smoker 03-08-2010 07:05 AM

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

deostroll 03-08-2010 07:28 AM

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.

smoker 03-08-2010 07:32 AM

Is that a question ?

Check my link again, it tells you how to run the command in the background so your script can finish.

deostroll 03-08-2010 07:53 AM

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...

smoker 03-08-2010 08:04 AM

You missed off the & at the end of the line.

Look at the example again.

deostroll 03-08-2010 08:15 AM

something wrong...the output file says
Code:

nice: java -cp jaminid.jar:LanguageTool.jar \
de.danielnaber.languagetool.server.HTTPServer: No such file or directory


smoker 03-08-2010 08:56 AM

1) You don't need nice
2) why did you put single quotes around the command ? You didn't need them before.

tuxdev 03-08-2010 09:17 AM

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

deostroll 03-08-2010 09:44 AM

@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...

catkin 03-08-2010 10:03 AM

Quote:

Originally Posted by deostroll (Post 3890318)
@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.


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