1

for example I have this script that kills firefox

ps x | grep firefox | grep -v grep | awk '{print $1}'| xargs kill -9

but when there is no firefox running it spits out:

Usage:
  kill pid ...              Send SIGTERM to every process listed.
  kill signal pid ...       Send a signal to every process listed.
  kill -s signal pid ...    Send a signal to every process listed.
  kill -l                   List all signal names.
  kill -L                   List all signal names in a nice table.
  kill -l signal            Convert between signal numbers and names.

What is the easiest way to display error message?

1 Answer 1

5

Your command for detecting Firefox processes is horrible, it also includes processes with "firefox" in their command line (e.g. directory name). Use pidof firefox instead.

You can first save the PIDs, check for whether it's (non)empty and adjust the behavior:

pids=$(pidof firefox)
if [ -n "$pids" ]; then
    kill -9 $pids
else
    echo "No Firefox processes found"
fi

Or use killall instead if you do not need an error message:

killall firefox

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .