Make sure only one instance of program is running

Hi,
I created a procd service to run a script passing some parameters.
Now, I wanna ensure that user doesn't start the same service, from terminal or other ways.

For this I added the following code in my script,

pid=$(pgrep -f "openwisp_monitoring.*--mode send")
if [ "$pid" ]; then
  echoerr "A process to send data is already running"
  exit 1
fi

But, when I am calling /etc/init.d/openwisp_monitoring restart, I am getting this error message and the service is not starting.
What should be done in this case?

1 Like

Use some type of socket and check for existence by binding on it?

Doesn't procd do this automatically? See procd_lock and friends. You get flocked files in /var/lock/ for each service

2 Likes

Okay thanks, I will look for them