Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
if you post , output of pgrep then it would be easy using xargs –  Rahul Patil Jan 2 '13 at 12:08
2  
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...) –  Gert van den Berg Jan 2 '13 at 12:14
    
You do not need ps -ef | in front of pgrep/pkill. –  jofel Jan 2 '13 at 12:14
    
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 –  Gilles Jan 2 '13 at 23:21
add comment

3 Answers

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|improve this answer
    
+1 for mentioning locking :) –  peterph Jan 2 '13 at 13:16
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.