Tags: complex, guys, killing, linux, motivated, output, parsing, processes, programming, script, unix, writing
parsing ps -ax output and killing processes
On Programmer » Unix & Linux
8,040 words with 8 Comments; publish: Wed, 30 Apr 2008 14:12:00 GMT; (20062.50, « »)
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.
http://unix-linux.itags.org/q_unix-linux-programming_91458.html
All Comments
Leave a comment...
- 8 Comments

- "Rob" <rob.unix-linux.itags.org.nospam.com> wrote in message news:ctlQd.28350$uc.1458.unix-linux.itags.org.trnddc09...
> 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.
>
im reading the awk man pages, it looks like that's probably what
i should be looking at but talk about confusing :). oh, this is for
bash on redhat if that makes a difference.
im playing with something along the lines of
awk -F: '{ print $1 }' | ps -ax | grep xxx
to at least output the process numbers, but it doesn't return me to the
shell and i'm not sure what to do from there anyway :)
#1; Wed, 30 Apr 2008 14:13:00 GMT

- "Rob" <rob.unix-linux.itags.org.nospam.com> wrote in message news:yMlQd.28360$uc.5215.unix-linux.itags.org.trnddc09...
> "Rob" <rob.unix-linux.itags.org.nospam.com> wrote in message
> news:ctlQd.28350$uc.1458.unix-linux.itags.org.trnddc09...
> im reading the awk man pages, it looks like that's probably what
> i should be looking at but talk about confusing :). oh, this is for
> bash on redhat if that makes a difference.
> im playing with something along the lines of
> awk -F: '{ print $1 }' | ps -ax | grep xxx
>
> to at least output the process numbers, but it doesn't return me to the
> shell and i'm not sure what to do from there anyway :)
>
it just occured to me the above example isn't doing anything more than
the ps -ax | grep xxx is :) :). oops!
#2; Wed, 30 Apr 2008 14:14:00 GMT

- "Rob" <rob.unix-linux.itags.org.nospam.com> wrote in message
news:lOlQd.28361$uc.27028.unix-linux.itags.org.trnddc09...
> "Rob" <rob.unix-linux.itags.org.nospam.com> wrote in message
> news:yMlQd.28360$uc.5215.unix-linux.itags.org.trnddc09...
> it just occured to me the above example isn't doing anything more than
> the ps -ax | grep xxx is :) :). oops!
>
new direction ... i just found pgrep and pkill commands :).
#3; Wed, 30 Apr 2008 14:15:00 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
#4; Wed, 30 Apr 2008 14:16:00 GMT

- In article <U6mQd.6906$m31.86596.unix-linux.itags.org.typhoon.sonic.net>,
Icarus Sparry <usenet.unix-linux.itags.org.icarus.freeuk.com> wrote:
>tokill=$(ps -ax | grep -- "$1" | grep -v grep | awk '{print $1}')
>if [[ -n $tokill ]]
>then
> kill $tokill
>fi
The first line could simplified as
tokill=$(ps -ax | awk "/[${1:0:1}]${1:1}/ { print \$1 }")
--
For low fair air travel, take Independence Air - http://www.flyi.com/.
Michael Wang * http://www.unixlabplus.com/ * mwang.unix-linux.itags.org.unixlabplus.com
#5; Wed, 30 Apr 2008 14:17:00 GMT

- <previous content deleted>
Thanks a TON for that primer, it's a bit confusing to me, but it will come
to me. I ended up using
pkill as you probably saw in another of my threads, but what you've shown me
I can already think
of a million uses for.
#6; Wed, 30 Apr 2008 14:18:00 GMT

- On Tue, 15 Feb 2005 12:57:10 GMT,
Michael Wang <mwang.unix-linux.itags.org.unixlabplus.com> wrote:
> The first line could simplified as
> tokill=$(ps -ax | awk "/[${1:0:1}]${1:1}/ { print \$1 }")
"ps axc" would be easier to parse as the fifth field will then be the
complete basename of the program name without arguments. Similar "ps -e"
for SystemV flavour.
ps axc |awk "\$5 == \"$1\" { print \$1}"
ps -e |awk "\$4 == \"$1\" { print \$1}"
As long as the process isn't selected based on arguments there would be
no reason for ps to provide this information.
Villy
#7; Wed, 30 Apr 2008 14:19:00 GMT

- On Tue, 15 Feb 2005 18:23:01 GMT,
Michael Wang <mwang.unix-linux.itags.org.unixlabplus.com> wrote:
> In article <slrnd140o8.294.vek.unix-linux.itags.org.station02.ohout.pharmapartners.nl>,
> Villy Kruse <nobody> wrote:
> If $1 is the command, then
> ps -e |awk "/[0-9] $1/ { print \$1 }"
> would do.
Or use pure shell:
ps -e | while read PID TTY TIME CMD
do
case $CMD in
$1) echo $PID ;;
*) ;;
esac
done
ps axc | while read PID TTY STAT TIME COMMAND
do
case $COMMAND in
$1) echo $PID ;;
*) ;;
esac
done
Villy
#8; Wed, 30 Apr 2008 14:20:00 GMT