84

I tried this code and it is not working

#!/bin/sh

#Find the Process ID for syncapp running instance

PID=`ps -ef | grep syncapp 'awk {print $2}'`

if [[ -z "$PID" ]] then
Kill -9 PID
fi

It is showing a error near awk.

Any suggestions please.

Paul R
  • 208,748
  • 37
  • 389
  • 560
user1597811
  • 1,033
  • 1
  • 12
  • 18
  • 4
    Why not just use `killall syncapp` ? – Paul R Dec 17 '12 at 07:42
  • What if there is no process running? Does it give any error? – user1597811 Dec 17 '12 at 07:44
  • 1
    See: [man killall](http://linux.die.net/man/1/killall) – Paul R Dec 17 '12 at 07:45
  • This process of killing is not helping me. Can you suggest how to repair the above method. Thanks – user1597811 Dec 17 '12 at 09:32
  • PID=`ps -ef | grep syncapp 'awk {print $2}'` I couldn't able to get the output which is actually process id into PID variable. Is there any syntax wrong or script wrongly written in this line of code. – user1597811 Dec 17 '12 at 10:14
  • On Linux and some other systems, `killall` is definitely the way to go, but be aware that on certain systems (Solaris for one), `killall` is completely different and basically means "ignore any arguments I was given and really kill everything"... – twalberg Oct 02 '13 at 14:39
  • See also [this similar question](http://stackoverflow.com/questions/3510673/find-and-kill-a-process-in-one-line-using-bash-and-regex) – Aaron Nov 04 '15 at 15:48
  • best way to check if process exists: http://stackoverflow.com/questions/3043978/how-to-check-if-a-process-id-pid-exists – Trevor Boyd Smith Jun 07 '16 at 15:43
  • pgrep processname && pkill processname || echo "Process not running" – chandank Jan 11 '18 at 20:25
  • @chandank Echo will also execute if `pkill` fails. – konsolebox Jan 04 '22 at 06:43

11 Answers11

202

Actually the easiest way to do that would be to pass kill arguments like below:

ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill
starball
  • 20,030
  • 7
  • 43
  • 238
Anthony Mutisya
  • 2,196
  • 1
  • 14
  • 5
  • 6
    `grep "your_process" | awk '{print $2}'` can be simplified to just `awk '/your_process/{print $2}'` – ssmith Jan 12 '14 at 02:42
  • 2
    This runs the (small) risk of killing the `grep` process before it kills the process you want to kill. To remedy that, use `awk '/[y]our_process/{print $2}' | xargs kill`, but using `killall` is better. – reinierpost Mar 13 '15 at 11:07
  • 4
    This might give problem because ps -ef might also give "grep --color=auto". So use `ps -ef | grep "your_process" | awk '{print $2}' | grep -v 'grep' | xargs kill` – Sumeet Patil Jul 30 '15 at 07:42
  • 1
    Didn't work for me, but if it's tweaked a bit: `kill -9 $(ps -ef | grep "your process" | grep -v grep | awk '{print $2}')` it works like a charm ;) – Nir Alfasi Jan 27 '18 at 04:37
  • 1
    All the comments after @reinerpost's are going in precisely the wrong direction. This is a common antipattern; the proper solution is to use a regex which doesn't match itself, and using Awk means you can and should avoid `grep` because Awk can do everything `grep` can do without a separate process, which could be quite significant for a tool you will need to run in situations where the system is starved for memory, processes, or CPU. – tripleee Feb 15 '18 at 12:30
  • As an aside, `kill -9` is also an antipattern. Killing a process happens with just `kill` and adding the `-9` is a violent and desperate measure which should always be avoided unless you know for a fact that the process you are trying to kill is buggy (and even then try without the `-9` first). – tripleee Feb 15 '18 at 12:32
  • you have to exclude both the running grep process and your own process. I put the command in a script and excludes both like this: ` ps -ef | grep "$1" | grep -v 'grep' | grep -v "$0" | awk '{print $2}' | xargs kill ` – Oscar van der Meer Sep 19 '21 at 21:49
22

This works good for me.

PID=`ps -eaf | grep syncapp | grep -v grep | awk '{print $2}'`
if [[ "" !=  "$PID" ]]; then
  echo "killing $PID"
  kill -9 $PID
fi
N Dierauf
  • 361
  • 2
  • 7
16

I use the command pkill for this:

NAME
       pgrep, pkill - look up or signal processes based on name and 
       other attributes

SYNOPSIS
       pgrep [options] pattern
       pkill [options] pattern

DESCRIPTION
       pgrep looks through the currently running processes and lists 
       the process IDs which match the selection criteria to stdout.
       All the criteria have to match.  For example,

              $ pgrep -u root sshd

       will only list the processes called sshd AND owned by root.
       On the other hand,

              $ pgrep -u root,daemon

       will list the processes owned by root OR daemon.

       pkill will send the specified signal (by default SIGTERM) 
       to each process instead of listing them on stdout.

If your code runs via interpreter (java, python, ...) then the name of the process is the name of the interpreter. You need to user the argument --full. This matches against the command name and the arguments.

guettli
  • 25,042
  • 81
  • 346
  • 663
  • super simple and works. I had to do "sudo pkill -9 coreaudiod" for it to work – n13 Feb 22 '21 at 07:57
  • 1
    Just a note, I had a Java process I wanted to kill, but the **--full** option did not exist for my version of pkill, however **-f** worked as expected, this was on CentOS 6.4. – ptha Apr 15 '21 at 10:05
  • This is the correct answer. Read the man page properly and you'll find plenty of useful options -U which restricts to specific users etc. – smaurice May 23 '21 at 02:02
5

You probably wanted to write

`ps -ef | grep syncapp | awk '{print $2}'`

but I will endorse @PaulR's answer - killall -9 syncapp is a much better alternative.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Unfortunately I didn't find the process name exactly. I'm getting the processes by using grep "sym". sym is the script name which start the process. So it is better for me to use the above method to kill the process. – user1597811 Dec 17 '12 at 09:31
  • PID=ps -ef | grep syncapp 'awk {print $2}' I couldn't able to get the output which is actually process id into PID variable. Is there any syntax wrong or script wrongly written in this line of code. – user1597811 Dec 17 '12 at 10:42
  • @user1597811: What you wrote is very different from what I wrote :p Check where all the quotes, and where all the pipe symbols (`|`) are. – Amadan Dec 17 '12 at 23:52
2

This should kill all processes matching the grep that you are permitted to kill.

-9 means "Kill all processes you can kill".

kill -9 $(ps -ef | grep [s]yncapp | awk '{print $2}')
EvR2f
  • 431
  • 4
  • 7
  • 2
    No, `-9` means send signal 9 instead of the regular 15. The precise semantics are that this untrappable signal terminates the process without giving it the chance of running any cleanup code, which is often set up via a signal handler which runs on a *trappable* signal. – tripleee Feb 15 '18 at 12:34
1

A lot of *NIX systems also have either or both pkill(1) and killall(1) which, allows you to kill processes by name. Using them, you can avoid the whole parsing ps problem.

jbr
  • 6,198
  • 3
  • 30
  • 42
  • 1
    That doesn't work if a process is running under java. I can't use 'killall glassfish' for exemple. I need to use killall java, but it would kill all programs running under java. – Jonathas Pacífico Dec 02 '13 at 11:49
  • 1
    For java processes, you can parse the value from jps and use kill the java process `jps | grep "Process Name" | cut -d " " -f "1" | xargs kill -KILL` – punitjajodia Jul 11 '14 at 12:10
  • If your code runs via interpreter (java, python, ...) then the name of the process is the name of the interpreter. You need to user the argument `--full`. This matches against the command name and the arguments. – guettli May 26 '16 at 10:18
0

Came across somewhere..thought it is simple and useful

You can use the command in crontab directly ,

* * * * * ps -lf | grep "user" |  perl -ane '($h,$m,$s) = split /:/,$F
+[13]; kill 9, $F[3] if ($h > 1);'

or, we can write it as shell script ,

#!/bin/sh
# longprockill.sh
ps -lf | grep "user" |  perl -ane '($h,$m,$s) = split /:/,$F[13]; kill
+ 9, $F[3] if ($h > 1);'

And call it crontab like so,

* * * * * longprockill.sh
user3743785
  • 33
  • 1
  • 4
  • Using `grep | perl` is a novel [useless use of `grep`](http://www.iki.fi/era/unix/award.html#grep) -- of course Perl already knows how to do `next unless /regex/;` before the part which attempts to parse the input line. – tripleee Feb 15 '18 at 12:36
0
#!/bin/sh

#Find the Process ID for syncapp running instance

PID=`ps -ef | grep syncapp 'awk {print $2}'`

if [[ -z "$PID" ]] then
--->    Kill -9 PID
fi

Not sure if this helps, but 'kill' is not spelled correctly. It's capitalized.

Try 'kill' instead.

0
Kill -9 PID

should be

kill -9 $PID

see the difference?

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
-1

Try the following script:

#!/bin/bash
pgrep $1 2>&1 > /dev/null
if [ $? -eq 0 ]
then
{
    echo " "$1" PROCESS RUNNING "
    ps -ef | grep $1 | grep -v grep | awk '{print $2}'| xargs kill -9
}
else
{
    echo "  NO $1 PROCESS RUNNING"
};fi
kenorb
  • 155,785
  • 88
  • 678
  • 743
-2
PID=`ps -ef | grep syncapp 'awk {print $2}'`

if [[ -z "$PID" ]] then
**Kill -9 $PID**
fi
usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
Sam
  • 1