How can i make sure there is only one my programme is running at the same time
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.
How can i make sure there is only one my programme is running at the same time
I have writed a programe a.out,I can run it many times, so there are lots of running a.out in the systerm.I want to make sure there is only one a.out is running at the same time?
Distribution: Fedora, mostly. Testing SUSE, Testing eee pc
Posts: 9
Rep:
One way to make the issue less confusing is to call your program something other than a.out. To do that, use the -o tag. for example gcc -o hello hello.c
You could also run a script to see if your program is already running. For instance,
the command ps shows all processes, or ps -e shows all processes for all users.
Therefor, ps -e | grep hello would return any process containing hello and ps -e | grep -q hello would just set a condition that you could test before
you start an instance of hello.
Distribution: Debian for Server / Ubuntu for Desktop
Posts: 14
Rep:
A better way to do it is to store a file somewhere (in /tmp for example) that indicates a running instance of your executable. Once your program has finished its job it must delete it.
So the pseudo-code would be :
Code:
main(){
if there is no file called /tmp/myexec.run {
create a file called /tmp/myexec.run
...
...
delete the file.
}
else; // An instance is running. Nothing to do (or maybe print a message)
}
Usually programs running as root store this file in /var/run/myprocess.pid. You can take a look at some of them.
Distribution: Fedora, mostly. Testing SUSE, Testing eee pc
Posts: 9
Rep:
I agree with Hko, given that you have the source for your program. Since you already said that, I DID know. Still, name your program something other than a.out, too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.