?
Solved

adding job to init.d tasks

Posted on 2003-03-06
10
Medium Priority
?
268 Views
Last Modified: 2013-12-15
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
0
Comment
Question by:dendenners
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 1

Expert Comment

by:LamerSmurf
ID: 8080620
Hey Denis,

try 'man chkconfig' :)

/LamerSmurf
0
 
LVL 1

Expert Comment

by:LamerSmurf
ID: 8080624
it might be a RH only thing, but give it a shot ;)
0
 

Author Comment

by:dendenners
ID: 8081015
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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 8

Expert Comment

by:heskyttberg
ID: 8086797
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
 

Author Comment

by:dendenners
ID: 8087088
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
 
LVL 8

Expert Comment

by:heskyttberg
ID: 8087122
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
 
LVL 8

Accepted Solution

by:
heskyttberg earned 140 total points
ID: 8087130
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
 

Author Comment

by:dendenners
ID: 8087689
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
 
LVL 8

Expert Comment

by:heskyttberg
ID: 8087935
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
 

Author Comment

by:dendenners
ID: 8088225
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

Featured Post

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month9 days, 23 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question