pgrep detect duplicate processes

view story

http://stackoverflow.com – I am trying to use pgrep to first detect if a process is running and if so, determine if the process has been duplicated (run) accidentally. If so, it should it kill the duplicate processes. if ! pgrep -f "java" > /dev/null; then echo "Yes its running but there may be duplicates" < Now check for duplicates here and kill if necessary > fi Please help * UPDATE * ps aux: debian 2521 3.8 5.4 407212 27524 pts/0 Sl 20:16 0:08 java -cp somefile.jar Code: if [ $(pgrep -f "somefile.jar" | wc -l) -gt 1 ]; then kill $(pgrep -f "somefile.jar" | grep -v $(pgrep -o "somef (HowTos)