# Question about writing a daemon in C



## overmind (Jan 15, 2010)

Hello,

I have some question regarding programming a daemon. Do I need to write pid of my process in /var/run/app.pid ?

I know it helps at restarting the daemon but how is better, with or without that file?


----------



## SirDice (Jan 15, 2010)

As you said, it helps restarting/stopping the daemon. From the point of view of your program it really doesn't matter.


----------



## Alt (Jan 15, 2010)

Also it helps detecting when your program already running, preventing double-start etc


----------



## expl (Jan 15, 2010)

.pid file is created mostly for usage by shell scripts.


----------



## redbrain (Feb 10, 2010)

Generally what you will find is this isn't handled via the actual program itself but you can get your programs pid from libc at runtime.

http://fuse4bsd.creo.hu/localcgi/man-cgi.cgi?getpid+2

Generally the init-scripts are simply a script which will start stop restart and do any invoke's of the actual program so you never touch the binary.

So this means the init-script will get/read the pid and then touch a file with the pid etc. Just take a look at a few init-scripts each os has there own way of doing them and they are very standardised.


----------

