• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shell script to restart a process

 
kannan vinayagam Duraiswamy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
/home/kannnan/start_<process>


 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11914
209
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic