Hi All,
Can anyone help me with the script problem.
I am wanting to find multiple proccess and then produce a true exit code if they are all there and a false if they are not.
I have been looking into this but can find no easy way of doing this.
I currently have this bit of code for testing.
# ps -fe| awk '{print $8}' | egrep "test1" -e "test2" |echo $?
This does not work as it will look for test1 and tehn test2 I need this stament but using test1 and test2.
Any help would be great
Paul
On 02Nov2006 08:45, Paul Ward pnward@googlemail.com wrote: | # ps -fe| awk '{print $8}' | egrep "test1" -e "test2" |echo $?
Maybe this:
out=`ps -fe| awk '{print $8}' | egrep "test1" | egrep "test2"` [ -n "$out" ]
If any lines get through both greps, $out will not be empty. This we test for.
Note that this doesn't scale very well to lots of tests. You'll have to do something more sophisticated for lots of tests.
Cheers,
Unfortunately this does not work as it greps for test2 on the output of the test1 grep which returns no output.
The command needs to grep for test1 and test2 not either or
On 02/11/06, Cameron Simpson cs@zip.com.au wrote:
On 02Nov2006 08:45, Paul Ward pnward@googlemail.com wrote: | # ps -fe| awk '{print $8}' | egrep "test1" -e "test2" |echo $?
Maybe this:
out=`ps -fe| awk '{print $8}' | egrep "test1" | egrep "test2"` [ -n "$out" ]
If any lines get through both greps, $out will not be empty. This we test for.
Note that this doesn't scale very well to lots of tests. You'll have to do something more sophisticated for lots of tests.
Cheers,
Cameron Simpson cs@zip.com.au DoD#743 http://www.cskk.ezoshosting.com/cs/
Men are not hanged for stealing horses, but that horses may not be stolen. - Lord Halifax, Works
-- fedora-list mailing list fedora-list@redhat.com To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
On 02Nov2006 10:11, Paul Ward pnward@googlemail.com wrote: | Unfortunately this does not work as it greps for test2 on the output | of the test1 grep which returns no output.
That's the idea.
| The command needs to grep for test1 and test2 not either or
So you want to test for test1 OR test2 on each line, not test1 AND test2?
You could run ps into a file, then loop over each test:
tmpfile=/tmp/pstest$$ ps ... >"$tmpfile" ok=1 for test in test1 test2 test3 ... do egrep "$test" "$tmpfile" >/dev/null || { ok=; break; } done rm "$tmpfile" [ $ok ]
Cheers,
On Thu, Nov 02, 2006 at 08:45:45AM +0000, Paul Ward wrote:
I am wanting to find multiple proccess and then produce a true exit code if they are all there and a false if they are not.
I have been looking into this but can find no easy way of doing this.
[ -n "$(pidof test1)" ] && [ -n "$(pidof test2)" ] && exit 0 || exit 1
or, if you had a list of pids:
for i in $pid ; do [ -n "$(pidof $i)" ] || exit 1 ; done
HTH, Tim
Paul Ward wrote:
... I am wanting to find multiple proccess and then produce a true exit code if they are all there and a false if they are not. ...
It might make things simple if you use ps to do the selection as well. Since you seem to know the PIDs you're looking for, you can ask ps to look for them specifically. Unfortunately, it won't return an error if any are missing, but you can have it print the ones it finds, and see if you get the same number out as you put in:
------- snip ------- #!/bin/sh
# the PIDs we want to check PIDS='2530 2514 4016 4017'
XPIDS=`ps --no-headers -o pid -p "$PIDS"`
if [ `echo "$XPIDS" | wc -l` = `echo $PIDS | wc -w` ] then echo "All PIDs present: ($PIDS)" else echo "Something's missing: need ($PIDS), found (`echo $XPIDS`)" fi ------- snip -------
I tried to stick with generic sh and avoid any bash-isms.
Here's what it looks like on a run:
$ sh -x tmp/fpids.sh + PIDS='2530 2514 4016 4017' ++ ps --no-headers -o pid -p '2530 2514 4016 4017' + XPIDS=' 2514 2530 4016' ++ echo ' 2514 2530 4016' ++ wc -l ++ echo 2530 2514 4016 4017 ++ wc -w + '[' 3 = 4 ']' ++ echo 2514 2530 4016 + echo 'Something'''s missing: need (2530 2514 4016 4017), found (2514 2530 4016)' Something's missing: need (2530 2514 4016 4017), found (2514 2530 4016)