4

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

4 Answers4

3

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

Rahul Patil
  • 24,711
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.

BitsOfNix
  • 5,117
2

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.

jofel
  • 26,758
0

if you have java crons and want to check before execution of that cron then try this : System.out.println("Checking for previous sessions for this cron: "+cronname);

            String proname=ManagementFactory.getRuntimeMXBean().getName();
            String pid=proname.split("@")[0];
            System.out.println("Current PID:"+pid);

            Process proc = Runtime.getRuntime().exec(new String[]{"bash","-c"," ps aux | grep "+cronname+" | awk '{print $2}' "});

            BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String s = null;
            String killid="";

            while ((s = stdInput.readLine()) != null ) {                                        
                if(s.equals(pid)==false)
                {
                    killid=killid+s+" ";    
                }
            }
            System.out.println("Running PID'S except current:"+killid);

            proc.waitFor();             

            Process proc1 = Runtime.getRuntime().exec(new String[]{"bash","-c","kill -9 "+killid});
            proc1.waitFor();                

            System.out.println("Previous duplicate sessions closed for: "+cronname);        

pass cronname as ur cronname.jar