adding job to init.d tasks

Hi,
I want to get my machine to automatically start a certain shell script on startup. The shell script sets a few variables and starts a java program that I want to run continuously. I can get the actual service to start (using 'service foo start' successfully (if I get the java program to run in the background in the script by prefixing the command with &) However, the stopping is proving more problematic, as I'm not quite sure what mechanism I should use to tell the service what the pid of the service to stop should be (because the actual called shell script ends if the java program is put into the background). Does anyone know how I should approach this problem, or better still have links to any online resources about how to setup this kind of service?
Thanks
Denis
dendennersAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
heskyttbergConnect With a Mentor Commented:
Hi!

I'm sorry it should be:
java -D MyFoo1=1 xx.xx.MyFoo
java -D MyFoo2=2 xx.xx.MyFoo

Or seomthing else that is easy to match on.

Regards
/Hans - Erik Skyttberg
0
 
LamerSmurfCommented:
Hey Denis,

try 'man chkconfig' :)

/LamerSmurf
0
 
LamerSmurfCommented:
it might be a RH only thing, but give it a shot ;)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
dendennersAuthor Commented:
I've already setup the service with chkconfig - the problem is the startup script itself. The problem is like this: say I have a shell script like this:
#! /bin/sh

class_path=.:/usr/foo/bar

java -classpath $class_path foo.bar.MyFoo &

So all it does is run the java MyFoo program in the background. However, the fact that MyFoo is in the background means that the actual script itself ends rather quickly (and the background process continues). I want to store the pid of the java program so that I can successfully call the 'service foo stop' command whenever I want, and it will correctly clean up the process. There are probably quite a few hackey ways of doing this but I'm wondering is there any builtin functionality of the init process that I could use to elegantly stop this kind of process (or even an elegant way of starting it so that the stopping is not messy). Thanks
Denis
0
 
heskyttbergCommented:
Hi!

If this is the only java program running at this time try this in your start/stop script:
cat /usr/sbin/pidof java > /var/run/MyFoo.pid
OR
ps -ef | grep MyFoo | awk '{ print $2 }' > /var/run/MyFoo.pid

Now in stop section you could do:
kill -s 8 `cat /var/run/MyFoo.pid`


Hope this helps

Regards
/Hans - Erik Skyttberg
0
 
dendennersAuthor Commented:
Thanks for the input. If there was some way of getting the full command from ps (or some other command) I would not have the 1 java program limit. I see that on my (Red Hat Linux) system, in the /proc/$pid/cmdline file the full command line is stored. Is there a way to access this via ps?
Thanks
Denis
0
 
heskyttbergCommented:
Hi!

Sorry if you missed that but my other recommendation was this:
ps -ef | grep MyFoo | awk '{ print $2 }' > /var/run/MyFoo.pid

It will work for many amny things.
If you have many MyFoo programs, why not start them like this:

java -D MyFoo1 xx.xx.MyFoo
java -D MyFoo2 xx.xx.MyFoo

Now you can do:
ps -ef | grep MyFoo1 | awk '{ print $2 }' > /var/run/MyFoo1.pid
ps -ef | grep MyFoo2 | awk '{ print $2 }' > /var/run/MyFoo2.pid

This is what we did in a Tomcat 3.x.x startup script where we wanted to run 20 instances.

ps -ef, extracts enough of the command line to be able to distinguish processes.

Regards
/Hans - Erik Skyttberg
0
 
dendennersAuthor Commented:
Thanks heskyttberg - I used this method - it would be nice if there was some way of getting the pid when you start the program though (so you could save it for later)
Denis
0
 
heskyttbergCommented:
Hi!

You can save it for later!

Just do this:
In your start stop script:
in the start) section you have somthing like:
java -D MyFoo1=1 xx.xx.MyFoo &

Directly under this line do:
ps -ef | grep MyFoo1 | awk '{ print $2 }' > /var/run/MyFoo1.pid

Now the pid for this process will be found in the file:
/var/run/MyFoo1.pid

So if you need it in another program or something just read that file, and in stop section of script you can do:
kill -s 8 `cat /var/run/MyFoo1.pid`

Regards
/Hans - Erik Skyttberg
0
 
dendennersAuthor Commented:
hi heskyttberg,
Sorry, that's what I am doing. What I meant was it would be nice if there was some way to store the pid directly after you run the command, instead of having to run a 'ps | grep ' type command. e.g., if there was some kind of shell variable that got filled with the pid of the last process executed ( a little like the way $? is filled with the return value of the last process executed). Obviously this isn't the case. Not to worry, the 'ps' solution will do fine.
Denis
0
All Courses

From novice to tech pro — start learning today.