Is there a good reason to write my own daemonize function instead of using daemon(3)?

后端 未结 4 862
孤街浪徒
孤街浪徒 2021-01-13 20:37

There are a lot of example implementations of daemons on the net. Most that I saw do not use the daemon(3) function to run the program in the background. Is that just a matt

4条回答
  •  情话喂你
    2021-01-13 20:50

    If you don't like any of the standard daemon() function actions, you might write your own. You can control whether it switches to the root directory; you can control whether it reconnects the standard I/O channels to /dev/null. But if you want to keep stderr open to a log file, while reconnecting stdin and stdout to /dev/null, you have to decide whether to use daemon() with appropriate options followed by other code is better than rolling your own.

    There isn't much rocket science in daemon(); it calls fork() and setsid() (according to the Linux version; the MacOS version mentions suspending SIGHUP while daemon() is operating). Check out the standard resources for more information on daemonization — for example:

    • W. Richard Stevens, Bill Fenner, Andrew M. Rudoff UNIX® Network Programming, Vol 1: The Sockets Networking API, 3rd Edn

    • Marc J Rochkind Advanced Unix Programming, 2nd Edn

提交回复
热议问题