0

I've written (well, remixed to arrive at) this Bash script

# pkill.sh
trap onexit 1 2 3 15 ERR

function onexit() {
    local exit_status=${1:-$?}
    echo Problem killing $kill_this
    exit $exit_status
}

export kill_this=$1
for X in `ps acx | grep -i $1 | awk {'print $1'}`; do
  kill $X;
done

it works fine but any errors are shown to the display. I only want the echo Problem killing... to show in case of error. How can I "catch" (hide) the error when executing the kill statement?

Disclaimer: Sorry for the long example, but when I make them shorter I inevitably have to explain "what I'm trying to do."

2
  • 1
    Is there a reason why you include signals 1,2,3,15 in your trap command? (These are HUP,INT,QUIT,TERM). Also, I'd recommend renaming "onexit" to "onerr", since that seems to be what you're using it for (from the signal ERR in your trap command). Commented Dec 22, 2009 at 15:09
  • Hi Ed. All good comments, +1. I copied the code (remixed, if you will), so I won't pretend that I analyzed those parts of it. THANKS Commented Dec 22, 2009 at 20:35

3 Answers 3

2
# pkill.sh
trap onexit 1 2 3 15 ERR

function onexit() {
    local exit_status=${1:-$?}
    echo Problem killing $kill_this
    exit $exit_status
}

export kill_this=$1
for X in `ps acx | grep -i $1 | awk {'print $1'}`; do
    kill $X 2>/dev/null
    if [ $? -ne 0 ]
    then
        onexit $?
    fi
done
Sign up to request clarification or add additional context in comments.

2 Comments

That worked, though I don't know what the 2> does instead of the semicolon in the original....?
Sorry, I get it: to redirect only stderr. Thanks!
2

You can redirect stderr and stdout to /dev/null via something like pkill.sh > /dev/null 2>&1. If you only want to suppress the output from the kill command, only apply it to that line, e.g., kill $X > /dev/null 2>&1;

What this does is take send the standard output (stdout) from kill $X to /dev/null (that's the > /dev/null), and additionally send stderr (the 2) into stdout (the 1).

4 Comments

Thanks Hank. I want the echo, I just don't want the output from the kill statement if it fails.
Then what if you change the line with the kill to kill $X > /dev/null 2>&1; ?
Hank: To redirect only stderr the proper way would be 2>/dev/null.
Thanks Hank, I'm marking this as plus one, marking Johannes' comment as +1 and marking Creasey's answer as best answer because it worked (though it came later).
0

For my own notes, here's my new code using Paul Creasey's answer:

# pkill.sh: this is dangerous and should not be run as root!
trap onexit 1 2 3 15 ERR

#--- onexit() -----------------------------------------------------
#  @param $1 integer  (optional) Exit status.  If not set, use `$?'
function onexit() {
    local exit_status=${1:-$?}
    echo Problem killing $kill_this
    exit $exit_status
}

export kill_this=$1
for X in `ps acx | grep -i "$1" | awk {'print $1'}`; do
  kill $X 2>/dev/null
done

Thanks all!

2 Comments

Just as a side note, for X in ps acx | grep -i "$1" | awk {'print $1'}; do could be better written as for X in $(ps acx | awk '/$1/ {print $1}); do
Thanks, I'll have to modify this script quite soon to incorporate your changes (which got eaten by the comments formatting) and the one on the question itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.