Creating a single instance application on Linux

The web shows me plenty of ways to create a single instance app, and I can't see the forest for the trees anymore. My application is a script that acts as a layer between domotica and a web interface. Due to the communication happening over a serial port, it's important only one app has access to the serial port at the same time.

What is the currently accepted "best" way to create a single instance application on Linux? There are a couple of requirements and constraints:

  • It's a Raspberry Pi, so the more lightweight, the better.
  • I'm running Raspbian 4.1.18.
  • I have full control over everything that runs, and everyone that has physical access to the machine. So I don't need to be able to solve situations where someone, for example, renames a locked file deliberately.
  • If another instance of my app is running, I want the new instance to shut down. It doesn't need to pass anything to the old instance.
  • I'd prefer if it was C89 and gcc compatible. But I suspect the code will be short enough to fix even if it's not.

  • Thanks in advance!

Edited by Saticmotion on
if the old instance crashes (or gets sigkill 9'ed) do you want the new instance to be able to start or is requiring a manual cleanup not a problem?
You could create (or open existing) file in /var/run/ folder when running. Then immediately acquire exclusive lock on it (flock + LOCK_EX|LOCK_NB). If lock succeeds, then you are first instance. If it doesn't - somebody is already running. No need to write/read anything to this file.

In case of fatal crash OS will unlock file and will allow next process to lock it again.
ratchetfreak
if the old instance crashes (or gets sigkill 9'ed) do you want the new instance to be able to start or is requiring a manual cleanup not a problem?


New instance should be able to start. But I hope my software is by now robust enough to not crash. Sigkills shouldn't happen.

mmozeiko
You could create (or open existing) file in /var/run/ folder when running. Then immediately acquire exclusive lock on it (flock + LOCK_EX|LOCK_NB). If lock succeeds, then you are first instance. If it doesn't - somebody is already running. No need to write/read anything to this file.

In case of fatal crash OS will unlock file and will allow next process to lock it again.


I've heard this before. But I've also seen many people say that this is really old-school and not very robust. But I know about your knowledge and experience, so I'm certainly reconsidering. And perhaps this is all I need for my use case.

Edited by Saticmotion on
I don't think there is "offical" way to do that. Everybody does whatever they want. It's pretty much the same on Windows. You just find some "global" object that you can create/lock/check and use it.
On Windows it could be windows class, named mutex/event or something else.
On Linux you can use this file locking, binding to unix domain socket, named semaphore or even named shared memory. Anything works. Using file is nice, because you can easily check if application is running & using it - if you cannot delete file, its running.

Although usually these kind of applications are created as services/daemons that are started by something like systemd. Then its up to the init system to guarantee that serivce is already running or not. They will also handle restarting of application if it crashes.
Alright, thank you! I went for the file locks. The behaviour of a daemon would be nice. But it's not applicable in my case. It's supposed to be a one off script that runs and scans the hardware in the domotica system. But if someone were to accidentally request a second scan while the first is still running, I need to catch that. File locks appear to work great for this situation.