Shell script to capture Process ID and kill it if exist

BashShellScripting

Bash Problem Overview


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.

Bash Solutions


Solution 1 - Bash

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

Hope it helps.

Solution 2 - Bash

This works good for me.

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

if [[ "" !=  "$PID" ]]; then
echo "killing $PID"
kill -9 $PID
fi

Solution 3 - Bash

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.

Solution 4 - Bash

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.

Solution 5 - Bash

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.

Solution 6 - Bash

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}')

Solution 7 - Bash

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

Solution 8 - Bash

#!/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.

Solution 9 - Bash

Kill -9 PID

should be

kill -9 $PID

see the difference?

Solution 10 - Bash

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

Solution 11 - Bash

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1597811View Question on Stackoverflow
Solution 1 - BashAnthony MutisyaView Answer on Stackoverflow
Solution 2 - BashN DieraufView Answer on Stackoverflow
Solution 3 - BashguettliView Answer on Stackoverflow
Solution 4 - BashAmadanView Answer on Stackoverflow
Solution 5 - BashjbrView Answer on Stackoverflow
Solution 6 - BashEvR2fView Answer on Stackoverflow
Solution 7 - Bashuser3743785View Answer on Stackoverflow
Solution 8 - BashAnonManonView Answer on Stackoverflow
Solution 9 - BashNot_a_UserView Answer on Stackoverflow
Solution 10 - BashchiranjeeviView Answer on Stackoverflow
Solution 11 - BashSamView Answer on Stackoverflow