How to kill only if process is running?

I usually just use pgrep and pkill

if pgrep myServer; then pkill myServer; fi

You could kill processes by name using pkill or killall, check their man pages.


A bit of a different approach would be something like this:

killall PROCESS || echo "Process was not running."

This will avoid the 1 Error-Code returned from the killall command, if the process did not exist. The echo part will only come into action, if killall returns 1, while itself will return 0 (success).