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).
xargs
ps -ef |
in front of pgrep/pkill.