LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-09-2010, 12:33 PM   #1
dstu
Member
 
Registered: Dec 2006
Posts: 37

Rep: Reputation: 0
Unhappy 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
 
Old 01-09-2010, 01:00 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,317

Rep: Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470Reputation: 4470
Quote:
Originally Posted by dstu View Post
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.

Last edited by TB0ne; 01-09-2010 at 01:03 PM.
 
Old 01-09-2010, 01:04 PM   #3
dstu
Member
 
Registered: Dec 2006
Posts: 37

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
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.
 
Old 01-09-2010, 01:41 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.

Last edited by colucix; 01-09-2010 at 01:42 PM.
 
Old 01-09-2010, 01:57 PM   #5
dstu
Member
 
Registered: Dec 2006
Posts: 37

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
... you can take a look at pkill... See man pkill for details.
pkill is exactly what I was looking for. Thanks.
 
Old 01-10-2010, 02:22 AM   #6
dstu
Member
 
Registered: Dec 2006
Posts: 37

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 08:35 AM
Shell-Script for killing processes oulevon Programming 4 02-14-2006 11:49 AM
Killing processes hongman Linux - Newbie 3 02-28-2005 06:27 PM
killing processes adriaanbw Linux - Software 3 05-24-2003 11:46 PM
Vm: Killing Processes tbhebe Linux - General 3 03-07-2002 10:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration