This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes shell script to restart a process Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "shell script to restart a process" Watch "shell script to restart a process" New topic

shell script to restart a process

kannan vinayagam Duraiswamy
Ranch Hand

Joined: Jan 12, 2009
Posts: 52
i try to write a shell script to restart a java process.
what are all needed to do,
need to get PID for the java process.
kill the java process
run the java process
i just get the PID like this,
PID=`ps-edf|grep <process_name>`.
what i get is like this,
axadmin 28699 1 0 14:00:50 pts/6 0:24 /tmp/<process_name> -client ..........

I need to get the '28699' form the above line.
can any one please guide me to finish the script

kannan vinayagam Duraiswamy
Ranch Hand

Joined: Jan 12, 2009
Posts: 52
finally i got it
here the solution,
PID=`ps -edf|grep <process_name>|awk '{print $2}'`
this will give the exact PID.
kill the process
kill -9 $PID
call the 'start' script to run the java process.

Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404

Some general suggestions ....

I believe that "ps auxwww" will work on more Unix systems than "ps -edf". (This may not be a problem for you).

The output of your ps command should contain both the <process_name> you are searching for, and the grep statement that is searching for that process name. So you are likely to have 2 lines returned from your grep statement. You can get around this by using egrep with a regular expression. For example, if I were searching for a process named catalina, I would probably run something like: "ps auxwww | egrep '[c]atalina'" - putting the c inside brackets means that it will match on the line that contains catalina as a single word, but not match on the egrep statement.

You do not need both a grep and an awk statement - awk can handle the "only process lines matching x" for you. So you could reduce this to "ps auxwww | awk '/[c]atalina/{print $2}'"

Some versions of Unix allow you to kill multiple processes in a single kill statement, which would allow you to have something like "kill -9 `ps auxwww | awk '/[c]atalina/{print $2}'`"

Other versions of Unix dont allow this - you might want to send output through xargs: "ps auxwww | awk '/[c]atalina/{print $2}' | xargs -n 1 kill -9"

Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Step by step ...
a) backticks are discouraged. Use better readable and nestable $( ) syntax instead:

b) As Andrew mentioned, getting the grep-command-PID is a pitfall. Gnu-ps allows to search immeadeately for a command:

c) Well - maybe there are just more options to ps? I.E. just return the PID?

(-Output: PID, = headline (empty))
d) How should you remember that funky syntax? -opid= -C ? Okay. Just use pidof:

e) we're getting close. An alternative is pgrep

f) You could call the PID together with the kill like this:

g) ... but still someone has foreseen that, and so let's introduce pkill:

is the solution.

Restarting has to go in a separate step, afaik.
Reading the man-page of pkill, pgrep etc. migth be a good inspiration for later.
It is sorta covered in the JavaRanch Style Guide.
subject: shell script to restart a process
Similar Threads
A method to close application window
Returning exit code to calling shell script
Get the UNIX process ID of the java program
Getting resin to start at bootup
error starting up 2nd instance of tomcat