Kill variables?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Kill variables?
# 1  
Old 11-18-2010
Kill variables?

Hi All,

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
# 2  
Old 11-18-2010
Code:
grep -v Proc ??? grep -v grep ???

By the way, did you try to manually enter this :
Code:
ps -ef | grep Proc | grep -v grep

... and see what result you have ?...

Do not mix the PID of the process and the UID of the user running it
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 11-18-2010
I apologize, I did not. like I said this is one of my first unix scripts.

I am unclear to what you are referring to.

---------- Post updated at 04:06 PM ---------- Previous update was at 04:03 PM ----------

I tried to do

Code:
kill -9 $Process

and it returned with

Code:
kill.sh[6]: awk: Arguments must be %job or process ids

# 4  
Old 11-18-2010
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

Last edited by ctsgnb; 11-18-2010 at 05:21 PM..
# 5  
Old 11-18-2010
I'm slightly confused where awk fits into the equation.

Rather than posting "pseudo code" (i.e. kill ----------) why not post the actual code?

As for grep X | grep -v X:

Pseudo code:
Code:
  find every line where X occurs
  remove every line where X occurs

Result = nothing.

Code:
$ kill -9 (followed by "nothing")
Usage: kill [-l] [-n signum] [-s signame] job ...

(and using kill -9 is not really a great idea - it doesn't give the process a chance to clean up properly).
# 6  
Old 11-18-2010
It returns the process ID, compared to doing a

Code:
ps -ef | grep <process>

where it returns the process ID, and the actual grep command itself.

---------- Post updated at 04:20 PM ---------- Previous update was at 04:19 PM ----------

Quote:
Originally Posted by scottn
I'm slightly confused where awk fits into the equation.

Rather than posting "pseudo code" (i.e. kill ----------) why not post the actual code?

As for grep X | grep -v X:

Pseudo code:
Code:
  find every line where X occurs
  remove every line where X occurs

Result = nothing.

Code:
$ kill -9 (followed by "nothing")
Usage: kill [-l] [-n signum] [-s signame] job ...

(and using kill -9 is not really a great idea - it doesn't give the process a chance to clean up properly).

Hi Scott,

I need to find out what to put after kill
# 7  
Old 11-18-2010
You should check the output of the commands as you go from the command line:

Code:
$ ps -ef | grep apache
 chubler 1470564  794750   0 07:06:27  pts/6  0:00 grep apache 
   httpd     576       1   0 16:36:02      -  0:00 /usr/local/apache2/bin/httpd -k start

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
  1. pipe the list to "grep -v grep" (everything not containing grep)
  2. 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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

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)
Discussion started by: Lakshman_Gupta
3 Replies

2. Shell Programming and Scripting

Kill an specific process ID using the KILL and GREP commands

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)
Discussion started by: enriquegm82
6 Replies

3. Solaris

Cannot kill a process with kill -9

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)
Discussion started by: adilyos
3 Replies

4. Linux

Kill a process without using kill command

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)
Discussion started by: sudhamacs
6 Replies

5. Shell Programming and Scripting

Kill a process without using kill command

Sorry, posted the question in other forum. (0 Replies)
Discussion started by: sudhamacs
0 Replies

6. Programming

How to convert byteArray variables to HexaString variables for Linux?

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)
Discussion started by: ritesh_163
2 Replies

7. UNIX for Advanced & Expert Users

Diff b/n kill and kill -9

Hi, I have a process with say pid x. What is the difference b/n kill x and kill -9 x in unix Thanks Ammu (2 Replies)
Discussion started by: ammu
2 Replies

8. Programming

kill(0,-9) don't kill the process

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)
Discussion started by: umen
9 Replies

9. UNIX for Dummies Questions & Answers

not able to kill find with kill -9

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)
Discussion started by: Amardeep
5 Replies

10. UNIX for Advanced & Expert Users

When kill doesnt work, how to kill a process ?

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)
Discussion started by: VijayHegde
3 Replies
Login or Register to Ask a Question