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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-09-2010, 11:33 AM
|
#1
|
Member
Registered: Dec 2006
Posts: 37
Rep:
|
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
|
|
|
01-09-2010, 12:00 PM
|
#2
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,792
|
Quote:
Originally Posted by dstu
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 12:03 PM.
|
|
|
01-09-2010, 12:04 PM
|
#3
|
Member
Registered: Dec 2006
Posts: 37
Original Poster
Rep:
|
Quote:
Originally Posted by TB0ne
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.
|
|
|
01-09-2010, 12:41 PM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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 12:42 PM.
|
|
|
01-09-2010, 12:57 PM
|
#5
|
Member
Registered: Dec 2006
Posts: 37
Original Poster
Rep:
|
Quote:
Originally Posted by colucix
... you can take a look at pkill... See man pkill for details.
|
pkill is exactly what I was looking for. Thanks.
|
|
|
01-10-2010, 01:22 AM
|
#6
|
Member
Registered: Dec 2006
Posts: 37
Original Poster
Rep:
|
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 12:01 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|