LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Creating Single Instance linux application (https://www.linuxquestions.org/questions/programming-9/creating-single-instance-linux-application-256693/)

praj_linux 11-18-2004 11:51 PM

Creating Single Instance linux application
 
Hi all,

I want to create a single instance application on linux.
On windows we can create events or mutex to check the already running instance. How to make it on linux.

Praj.

rjlee 11-19-2004 03:52 AM

By “single instance application”, I assume that you mean a program that can only be run once, and if run again a new instance will not start?

The traditional way to do this is to have the program write out its PID (process ID) to a lock file as soon as it starts up, unless the lockfile already exists.

If the lockfile already exists, then the process should read the PID from it instead. It should then check the file /proc/pid/cmdline, and extract the name of the running process. If it is the same as the name of the current process, then the lockfile should be deleted, and it should try again. Otherwise, it should exit.

There's probably a library that does this for you.

If the program could be run by a different user (and you want to prevent that), then you would normally put the lock file in /tmp, using the various routines to make a temporary file.

Hope that helps,

—Robert J. Lee

Hko 11-19-2004 04:06 AM

Here's anopther thread about this.
http://www.linuxquestions.org/questi...hreadid=242069
It's about a lock file. It should be possible using a semaphore as well (see "man semctl").


All times are GMT -5. The time now is 01:05 PM.