You are not logged in.

#1 2012-09-27 04:40:35

Hspasta
Member
Registered: 2011-12-24
Posts: 187
Website

[SOLVED]Allowing One Instance by PID File

I'm writing a daemon program that uses a PID file to detect if there is an instance running or not. This is how I am doing it.

	int pid_file;

	if ((pid_file = open("var/run/simpfand.pid", O_CREAT | O_EXCL)) == -1) {
			printf("simpfand: another instance running! "
			       "Do not manually run.\n");
			return 1;
        }
        ...
        close(pid_file);
	

It works fine, but if I wanted to restart the daemon, the PID file is still there and I have to manually delete it. I thought close would delete the file, but it doesn't. How do I get rid of this PID fiile?

Last edited by Hspasta (2012-09-29 06:21:45)

Offline

#2 2012-09-27 05:37:13

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 696
Website

Re: [SOLVED]Allowing One Instance by PID File

Offline

#3 2012-09-27 15:25:58

Hspasta
Member
Registered: 2011-12-24
Posts: 187
Website

Re: [SOLVED]Allowing One Instance by PID File

I guess I'm not really writing a daemon. I'm letting systemd do the running for me. I don't plan on calling fork() in my program.

Offline

#4 2012-09-28 04:59:41

djgera
Developer
From: Buenos Aires - Argentina
Registered: 2008-12-24
Posts: 696
Website

Re: [SOLVED]Allowing One Instance by PID File

Oh in such case, pidfile is not needed. You have this logic in systemd plus other lots of features wink

Offline

#5 2012-09-29 06:21:34

Hspasta
Member
Registered: 2011-12-24
Posts: 187
Website

Re: [SOLVED]Allowing One Instance by PID File

Oh. I think I was going about a problem wrong.

Thanks for the help.

Offline

Board footer

Powered by FluxBB