25

Currently, I use a line like this in my sh script file:

kill $(ps aux | grep -F 'myServer' | grep -v -F 'grep' | awk '{ print $2 }')

But I wonder how to call it only if process (myServer here) is running?

2
  • 1
    it's impossible to call kill only when the process is running, because there is a race condition between the test and the invocation of kill (it's possible the process stopped for another reason in that short time). You should use pkill or killall which do exactly the same as what you try to do, but with less to type (and probably some other advantages too).
    – JanC
    May 10, 2016 at 12:26
  • Related: How to let 'kill' ignore processes that are not alive. Also, you can avoid ps+grep x2 with pgrep Jul 5, 2022 at 7:22

9 Answers 9

33

I usually just use pgrep and pkill

if pgrep myServer; then pkill myServer; fi
6
  • 10
    Why not pkill myServer or killall -q myServer? There's no downside to attempting to kill inexistent processes. Oct 3, 2014 at 15:57
  • 16
    It will not exit 0 if there is no process to kill, so it could break scripts. Oct 6, 2014 at 15:00
  • 12
    Append || true then. Oct 6, 2014 at 15:01
  • True, but you won't have the flexibility to add a else to inform you the process was already dead. Oct 6, 2014 at 16:57
  • What about if ! killall -q myServer; then echo ERROR; fi? Oct 6, 2014 at 17:07
10

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

1
  • yes, unlike kill, this kills only if process is running. For example pkill -u myuser -f myprocess\.[0-9]+
    – Chase T.
    Jun 26, 2017 at 18:50
9

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).

3
  • 8
    Using killall -q is better
    – Anwar
    Jun 27, 2017 at 15:51
  • 3
    Warning, killall -q isn't supported on all unix flavors, macOS Catalina still doesn't have it.
    – tresf
    Oct 4, 2019 at 14:36
  • 1
    @tresf my ubuntu distribution doesn't have it either
    – FooBar
    Sep 25, 2020 at 22:00
1

Check if the process exist with pidof. If it does, kill it:

(! pidof process_name) || sudo kill -9 $(pidof process_name)

Exit code is always 0 after executing the above command.

1

Use pkill with option -f

pkill -f myServer

Option -f is the pattern that is normally matched against the process name.

1
  • You can also put -2 before -f to shut the process down more gracefully (same as pressing Ctrl + C).
    – jobukkit
    Nov 6, 2023 at 17:52
1

Small Script I have created with R&D. I hope you will like it

#!/bin/bash

echo "Enter the process name:"
read $proc_name
if pgrep $proc_name; then
  echo "$proc_name running "
  pkill $proc_name
  echo "$proc_name got killed"
else
  echo "$proc_name is not running/stopped "
fi

save it with some name like script.sh then

chmod +x script.sh
./script.sh

Then give your process name.

Note: I have tried many times with this and its fine.

2
  • May I know whats wrong with this script and -ve vote needs an explanation please.
    – Raja G
    Oct 5, 2014 at 5:33
  • Nothing wrong with this script... +1 Nov 13, 2016 at 1:26
0
pkill -9 -f jboss || true

or

pkill -9 -f tomcat || true 
-1

try using:

sudo kill `pidof process_name`

where process_name is the name of the process you want to kill. What's great about this is that pidof will list the pid's of all processes matching the given name. Since kill accepts multiple pid's, it will kill of all of them in a single whim.

Let me know if this helps.

2
  • What if list is empty? (my main point was not to call kill at all if possible)
    – myWallJSON
    Jan 31, 2012 at 12:44
  • 1
    If the list is empty, kill will return an error code 1 and not kill anything. It would be the equivalent as trying to call kill with no arguments. You could add an if statement that first checks if pidof returns an empty list. If it does not return an empty list, call kill `pidof process_name. If it does return an empty list, skip the kill call.
    – amandion
    Jan 31, 2012 at 12:59
-1

I would use:

ps x --no-header -o pid,cmd | awk '!/awk/&&/myServer/{print $1}' | xargs -r kill

The xargs -r tells to run kill only if there is input. The ps --no-header -eo pid,cmd gets the output into a better format for parsing.

You must log in to answer this question.

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