"Strong Scripting Skills" - a definition? - sunday
Alvin Oga
alvin at Mail.Linux-Consulting.com
Sun Jan 25 16:14:45 PST 2004
hi ya jim
it's sunday... time for some fun
On Sun, 25 Jan 2004 jimd at starshine.org wrote:
> I once had an interviewer put up a wretched fragment of shell code like:
>
> kill `ps wax | grep "foo" | grep -v "grep" | awk '{print $11}'`
>
> ... and ask me to explain what was "wrong with it. I pointed out that
> I wasn't sure which field number the PID was in in that ps output
> format, and I always check with 'ps -$FLAGS | head' before code
> anything like that. (That was the answer he was looking for). Then
"ps waxu" has its pid in $11
"ps wax" has its pid in $5 ...
if foo is named " ThisSillyApp", one needs "grep -i"
if foo is an argument for another-app, one needs to watch out for
the other's command options that happen to be "foo"
if multiple instances of foo is running, you might be sol and/or
kill might kill all ( 2-n ) instances of "foo" which is good and bad...
bad if it was intended for "kill particular-sshd"
other times it's good, "kill all-hung-mozilla" (or hung konqueror)
too many pipes in the above ... bash barfs on "|" commands
cmd="ps axuw | grep sshd "
echo "$cmd"
$cmd -- sometimes doesnt work .. depends
I break out each pipe'd command separately in bash scripts
( think i'm missing an old trick someplace )
more importantly.. did one test that it returns a valid pid
-- always check that pid is non-zero and valid .. before killing
do you want kill -HUP or kill -SIGHUP or kill -9
what if kill doesnt kill it cause foo is stuck or still being used..
why not simplify it after verifying the pid is correct
kill `cat /var/run/app.pid`
kill ` pidof foo `
killall foo
sometimes grep -v grep is needed, sometimes not... i donno why some
greps does it differently
and always test it afterward to verify it did what its supposed to
in the script ( check the return val or other means to double check )
and the above ps options line is not portable to multiple *nix
c ya
alvin
More information about the Baylisa
mailing list