Killing all the process of a command except first process

5,943

Solution 1

Below Example of apache2 process, it will kill all PID's except first one

pgrep apache2 |awk 'NR >= 2' | xargs -n1  echo kill

if output of above command looks fine , then you can remove echo

Solution 2

you can try this out:

ps -e --sort=start -o start,pid,cmd|grep <cmd>|egrep -v grep |sed 1d

this will give you the following formatted output:

start time, PID, command being executed

for i in `ps -e --sort=start -o start,pid,cmd|grep <cmd>|egrep -v grep |sed 1d`
do
     kill -9 $i
done

replace the kill by echo first to ensure that it will kill the news ones.

Solution 3

You can do this for example with:

OLDEST_PID=$(pgrep -o 'DELETE OPERATION_CONTEXT')
test $OLDEST_PID && pgrep 'DELETE OPERATION_CONTEXT' | grep -vw $OLDEST_PID | xargs -r kill

The first line finds the oldest PID. The second line checks first if $OLDEST_PID contains something. If yes, it list all matching processes, filters the $OLDEST_PID out and kill them (if any remain).

A better way is to avoid duplicated processes by adding lockfiles to the cron script like in this question.

Share:
5,943

Related videos on Youtube

Ankit Vashistha
Author by

Ankit Vashistha

Updated on September 18, 2022

Comments

  • Ankit Vashistha
    Ankit Vashistha over 1 year

    I am sometimes stuck in a situation where a script/command kept in Cron runs more than once because of some reasons (the first instance is not completed fully, the second instance of the same process starts) and at some point of time these processes increase a lot and show system hanging etc. So, what I want as a temporary solution here is to check if there are more than one instances of the script/command say,

    ps -ef | pgrep -f 'DELETE OPERATION_CONTEXT'
    

    or directly kill all the processes of this command

    ps -ef | pkill -f 'DELETE OPERATION_CONTEXT'
    

    But I want an idea how I can kill all the processes of pgrepped command except for the first process (this would let one of the process to keep working and all other duplicates to get killed).

    • Admin
      Admin over 11 years
      if you post , output of pgrep then it would be easy using xargs
    • Admin
      Admin over 11 years
      Preventing the duplicates might be a better idea? Check if file exist with a valid PID at startup (possibly event check its name vs $0). If it is invalid or doesn't exist, create a file with the PID and run the script. Delete file when script is complete. (Finding that a file is missing is much quicker than reading its contents, checking if it is a valid PID, etc...)
    • Admin
      Admin over 11 years
      You do not need ps -ef | in front of pgrep/pkill.
    • Admin
      Admin over 11 years
      As Gert wrote, it would be easier and more useful to ensure that only one instance of the cron job is running. See Quick-and-dirty way to ensure only one instance of a shell script is running at a time