Make sure only one instance of program is running

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

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


Okay thanks, I will look for them