I'm writing an init.d script to start a program that I've written in C. The program follows a pretty standard template where it calls fork()
to create a child process, and then the parent process terminates almost immediately. The trouble I'm running into is that the PID file gets the parent process ID written to it, so later when I call "stop" it fails because the parent has long since ended and only the child is running.
Currently I have this command in my startup script:
start-stop-daemon --start --background -m --oknodo --pidfile "$PIDFILE" --exec "$SCRIPT" || return 2
I confess that I'm very new to all of this so I'm not even entirely sure what all those flags do. This was mostly a copy/paste from various articles I stumbled across. In any case, this would seem like a fairly common thing. How can I make it write the correct (i.e. child) process ID in the pid file?
Daemons need to create .pid file by own self. Because of daemonizing procedure by it self, when you change your start pid.
Option --pidfile
is for pid file which already created. Also start-stop-daemon has option --make-pidfile
, but it's useful only for program which not daemonize own self.
-m, --make-pidfile ... Note: This feature may not work in all cases. Most notably when the program being executed forks from its main process.