Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.