Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kill a process if it exists

I want to kill all instruments processes that are running. I am using this:

sudo killall instruments

I am using this in a script and sometimes the process is not running and it stops my script saying there are no processes with that name that are running.

How can I check if a particular process is running? Instruments in my case.

like image 752
SirRupertIII Avatar asked Jun 11 '13 16:06

SirRupertIII


2 Answers

If your script is terminating, you have most likely enabled set -e, to exit when a command fails.

If you don't care about the status, you can just append || true to the command:

sudo killall instruments || true
like image 140
that other guy Avatar answered Nov 15 '22 18:11

that other guy


You can use pgrep <proc> to search for a process named <proc>

if pgrep instruments &> /dev/null ; then sudo killall instruments ; fi
like image 40
giordano Avatar answered Nov 15 '22 18:11

giordano