Checking For Duplicated Processes In Korn Shell

A couple of days ago I was needing to solve an issue with our EAI framework. The implementation of such framework could be basically described as instances of Java programs running under Solaris, but there’s a particular instance that should be running just once. So I wrote this tiny shell script to avoid launching that program twice:

#!/bin/ksh
#
theProcess=someProcess
if [ $(pgrep -u $USER -f $theProcess | wc -l) -gt 0 ] then
echo "There is an instance of "$theProcess" already running."
else runTheProcess
fi

someProcess could be any regex that matches the process description,
runTheProcess is whatever command that executes the process.
Okay, that’s all, I hope you’ve found it informative and thanks for reading!

2 Responses to “Checking For Duplicated Processes In Korn Shell”


  1. 1 chubbs February 21, 2008 at 9:49 pm

    perhaps you should check it like so:


    if [ $(pgrep -u $USER -f $theProcess | wc -l) -gt 0 ]

    just in case

  2. 2 Nano Taboada February 23, 2008 at 7:57 pm

    Thanks for your comment chubbs! I’m modifying the original code in the post to include your suggestion! :)


Leave a Reply