Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script to capture Process ID and kill it if exist [duplicate]

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.

like image 705
user1597811 Avatar asked Dec 17 '12 07:12

user1597811


People also ask

What is the command to kill a process with process ID?

There are two commands used to kill a process: kill – Kill a process by ID. killall – Kill a process by name.

What is the command to kill a process with process ID as 100?

Kill a Process by the kill command To terminate a process, execute the kill command followed by PID. To locate the PID of a process, use the top or ps aux command, as explained above. To kill a process having PID 5296, execute the command as follows: kill 5296.


2 Answers

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.

like image 198
Anthony Mutisya Avatar answered Oct 02 '22 15:10

Anthony Mutisya


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 
like image 36
N Dierauf Avatar answered Oct 02 '22 15:10

N Dierauf