I am writing a part of a script that needs to grep for process ID's for a certain process, and then kill them if they are greater then 0. Here is what I have so far.
Code:
Process=$(ps -ef | grep Proc | grep -v Proc)
if [ "${Process}" -gt "0" ]
then
echo "Process Is not running"
else
kill -------------
fi
It works if I set it up and manually type in the process ID's, but me being a newb to this unix thing, I can't figure it out. Does anyone have any insight?
Thanks in advance!
Jeff
Last edited by Scott; 11-18-2010 at 05:05 PM..
Reason: Please use code tags
Pls just give me an example of output you have when typing this :
Code:
ps -ef | grep Proc | grep -v grep
indeed:
Code:
ps -ef | grep Proc | grep -v Proc
makes no sens : it means you retain only lines having "Proc" pattern in it, and then you filter out (-v option) all of those lines that have "Proc" pattern in it : you filter out just what you have filtered in : the result is you retain nothing : so $Process is just empty
First thing we see is the output has a number of fields (UID,PID,PPID, etc). Also note the grep process has picked it's self up "grep apache" contains agache too.
There are two common methods to avoid finding the grep command in your list
pipe the list to "grep -v grep" (everything not containing grep)
grep for [a]pache instead. Square brackets mean any single character from list, and in this case the list is a single "a". So again we are searching for apache but this time the grep command line dosn't contain apache.
Now you want the 2nd field from the listing (ie the PID) as you can see a variable number of spaces exists between UID and PID, this makes things hard for the cut command to fet the 2nd field and most use awk to do this job as it count multi spaces as 1 seperator.
The output will now be the PIDs or blank if none found. Use the -z option of test to check for blank strings. So putting it together to get a list of all command lines that contain your Proc you need:
Code:
Process=$(ps -ef | grep [P]roc | awk '{ print $2}')
if [ -z "$Process" ]
then
echo "Process Is not running"
else
kill $Process
fi
Last edited by Chubler_XL; 11-18-2010 at 05:34 PM..
Hi All,
i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way??
#!/bin/ksh
##script is sample.ksh
age=$1
gender=$2
class=$3
.
.
.... (3 Replies)
Good afternoon
I need to KILL a process in a single command sentence, for example:
kill -9 `ps -aef | grep 'CAL255.4ge' | grep -v grep | awk '{print $2}'`
That sentence Kills the process ID corresponding to the program CAL255.4ge.
However it is possible that the same program... (6 Replies)
Hello everyone,
I have a process that I want to kill. I have tried kill-9 PID but it doesn't work. I have tried preap PID but it doesn't work too.
The parent of my process is the process whose PID is 1, so I can't kill it.
My OS is a Solaris 9.
Can anyone help me understand what's going... (3 Replies)
I want to Kill a process without using kill command as i don't have privileges to kill the process. I know the pid and i am using Linux 2.6.9 OS. (6 Replies)
Hello everybody,
I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Hi all
i have simple c program , when i wish to kill the app
im using kill(0,-9) , but it seams this command don't do any thing and the program.
just ignore it .
what im doing wrong here ?
im using HP-UX ia64
Thanks (9 Replies)
Hello everyone
I am using HP Ux and had run a find command. Now I am trying to kill it with kill or kill -9 but it is not getting killed and still running. Any clues ?
Thanks
Sidhu (5 Replies)
Hi All,
I am unable to kill a process using kill command. I am using HP-UX system. I have tried with kill -9 and i have root privilages.
How can i terminate this daemon ? ? ?
Regards,
Vijay Hegde (3 Replies)