ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Not having a coding example handy, but you
could do a system( ps ) [on my box this
( ps -Ac --no-headers | grep your_progs_name )
would provide the desired information] and
terminate if it's already running?
It maybe a bit of a workaround, but could you make your program create a certain file as soon as it starts in /tmp or /var/run. And check before if the file exists, and if it does exit (as not to start another instance).
A good way to do this, is to open the file from the start of the program and not closing it anywhere. Instead delete (unlink) it immediately after opening.
The file will then be deleted when the program exits, or when it's killed. This way the file will also be deleted if the program is killed some day. And also it wouldn't matter is another program tries to delete it, while your program is running.
Instead of creating a lock file, I believe a better method
(under UNIX) is to use a semaphore -- with options that
the starting program creates a semaphore and that the
semaphore will allow only one program to increment it.
Dying program will clear its increment, so it will automagically
make the next program startup possible.
The SEM_UNDO option should be used for auto-clear of the
semop() increment on process exit.
Another possibility is indeed to create a file with the
pid of the process in it.
Any other process may open this file and check if the
process with given pid is still running. If not, it will remove
the file and will re-create it with own pid.
To avoid theoretically possible startup of more than one
process "at the *same* time", one should provide an
exclusive lock to the file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.