Re: parsing ps -ax output and killing processes

From: Icarus Sparry (usenet_at_icarus.freeuk.com)
Date: 02/15/05


Date: Tue, 15 Feb 2005 12:42:28 GMT

On Tue, 15 Feb 2005 11:58:00 +0000, Rob wrote:

> After the success of writing my first script yesterday (thanks guys!) I'm
> motivated to move on to something more complex today. How would I go about
> doing something like a
>
> ps -ax | grep xxxx
>
> and then killing any processes listed (except for the one running the
> command of course). There may not be any processes, I was thinking to
> detect that I could write out to a file and check the file size, but I
> guess that doesn't really help me if there are any processes listed.
> Basically I run a shutdown script which doesn't always work, then need to
> verify that the script did work by seeing if processes are still running
> and manually killing them if they arent.

It is more normal to use 'awk' here, rather than grep, as you need to not
only select particular lines, you then need to manipulate them.

Awk is pretty easy to use, and has all the searching of grep built in.

However since this is your second script, let us proceed in small steps.

You already know you want to do something like 'ps -ax | grep something'
and then manipulate the result. If there is nothing left then do nothing,
otherwise run kill on it. The 'test' command (also known as '[') can tell
if a variable is null or not, and it can also test to see if a file is
empty. Modern shells such as ksh, bash, zsh have a syntax element called
'[[' which handles things a little better.

So one thing you could do is

tokill=$(ps -ax | grep something | extract_just_the_first_column)
if [[ -n $tokill ]]
then
        kill $tokill
fi

A command for extract_just_the_first_column is
        awk '{print $1}'

You also say you don't want to kill off the commands doing the killing. A
way of doing this is to use 'grep -v pattern' which selects the lines
which do not match. putting this together

tokill=$(ps -ax | grep something | grep -v grep | awk '{print $1}')

If you want the "something" to be fixed, then you have it. Otherwise you
can use the positional parameters.

#!/bin/sh
tokill=$(ps -ax | grep -- "$1" | grep -v grep | awk '{print $1}')
if [[ -n $tokill ]]
then
        kill $tokill
fi



Relevant Pages

  • [opensuse] Add variables to standard output for
    ... output - the finger command) ... I want to take the standard output from a grep ... My script: ... The views expressed in this e-mail are the views of the individual sender and should in no way be construed as the views of the Company. ...
    (SuSE)
  • Re: new guy ksh scripting questions
    ... script to be called from the crontab of the Oracle user. ... command into a grep command looking for ALIVE, pipe that into a wc -l ... then echo good ...
    (comp.unix.aix)
  • Re: [opensuse] Add variables to standard output for
    ... output - the finger command) ... I want to take the standard output from a grep ... My script: ...
    (SuSE)
  • Re: bash: any way to reuse the last output?
    ... > manipulate the last output gets me by surprise, I will do, for instance: ... > (say, with grep) ... you can use script command. ...
    (Fedora)
  • Re: want to ask how to use grep -v repeatly
    ... i am a new user for unix, and is writing a script for working purpose. ... could someone tell how can I "grep -v " more than a string in ... a command, cause the machine i'm using is very busy. ... when I pipe the ...
    (comp.unix.shell)