LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script for killing several processes and then launching them again (https://www.linuxquestions.org/questions/linux-newbie-8/script-for-killing-several-processes-and-then-launching-them-again-781079/)

dstu 01-09-2010 11:33 AM

Script for killing several processes and then launching them again
 
Hello,

We use a Java application that crashes occasionally.

When it does, I have an external application that notices it and sends me an email, at which point, I need to kill the processes and then launch tomcat again.

I found on another forum this command:

Code:

kill -9 `ps -aef | grep 'tomcat' | grep -v grep | awk '{print $2}'`

I think it's suitable if the result of the ps is a single line, but in my case, I get 3 lines, so I don't know how to make it run all on 3 processes.

Also, I need to run afterwords the following command:
Code:

/etc/init.d/tomcat6 start
Can anyone help me with the script?

Thanks,

David

TB0ne 01-09-2010 12:00 PM

Quote:

Originally Posted by dstu (Post 3820286)
Hello,

We use a Java application that crashes occasionally.

When it does, I have an external application that notices it and sends me an email, at which point, I need to kill the processes and then launch tomcat again.

I found on another forum this command:

Code:

kill -9 `ps -aef | grep 'tomcat' | grep -v grep | awk '{print $2}'`

I think it's suitable if the result of the ps is a single line, but in my case, I get 3 lines, so I don't know how to make it run all on 3 processes.

Also, I need to run afterwords the following command:
Code:

/etc/init.d/tomcat6 start
Can anyone help me with the script?

If by "help", you mean "assist you because you've got a problem", then sure.

If by "help", you mean "can someone write the script for me?", then no...

As with all requests like this, post what you've written so far, and where you're getting stuck, and we'll be happy to help you. Otherwise, you can find many good bash scripting tutorials on Google, to help get you started. HINT: look at the "killall" command, and/or the "while" directive in bash scripting, to kill multiple PID's.

dstu 01-09-2010 12:04 PM

Quote:

Originally Posted by TB0ne (Post 3820326)
If by "help", you mean "can someone write the script for me?", then no...

The help I need is to modify the kill command to suit an execution of more than one result of the ps command.

Thanks.

colucix 01-09-2010 12:41 PM

In addition to what suggested by TB0ne, you can take a look at pkill. The line of code you found is totally superfluous, since you can force ps to print the needed information using the correct options (no need to grep, extract and so on). Moreover pkill joins the functionality of ps and kill together. A simple command can do the job. See man pkill for details.

dstu 01-09-2010 12:57 PM

Quote:

Originally Posted by colucix (Post 3820371)
... you can take a look at pkill... See man pkill for details.

pkill is exactly what I was looking for. Thanks.

dstu 01-10-2010 01:22 AM

pkill result needs to be checked
 
Hello,

I noticed that pkill doesn't always kill all the processes.

I think that I should include in the script a verification of pgrep's result and if there's still a process active, run pkill again.

What is the correct syntax for the following condition?

Code:

while [  **pgrep java** != "" ]; do
  pkill java
done
/etc/init.d/tomcat start

Thanks.


All times are GMT -5. The time now is 03:07 PM.