I humbly recommend doing such daemonizations from outside your program. Programs that daemonize on startup make it very difficult to monitor them by direct means, instead forcing you to rely on PID files and other mechanisms which may not always be available or fresh.<div>
<br></div><div>For reference, Upstart, the new PID 1 on Ubuntu and friends, has a horrible hack[1] built in specifically to keep track of processes that &quot;helpfully&quot; daemonize themselves, so that it can offer additional services like restarting crashed services or notifying the owner of a problem.</div>
<div><br></div><div>As has been pointed out elsewhere on thread, there are plenty of standalone programs that implement daemonization if you end up not using service management software to do it for you. It&#39;s also trivial to implement a Haskell program that would do this for you, since that standalone program would not need threads or the complications that they bring to forking.</div>
<div><br></div><div>Or if you really must, make daemonization a startup option of your real server, but please please please have a flag to turn that behavior off, so that your service isn&#39;t a nightmare to manage for us poor sysadmins :-).</div>
<div><br></div><div>- Dave</div><div><br></div><div>[1]: if you care, the hack in question is ptrace(): if you declare to Upstart that your service daemonizes itself, it will boot your binary, ptrace() it, and track the process across two fork/clone syscalls, then un-ptrace() and use the second child as the &quot;real&quot; service process. Please don&#39;t make binaries where daemonization can&#39;t be turned off. Machine management software should not have to use debugging facilities to correctly monitor your program.<br>
<br><div class="gmail_quote">On Sat, Mar 5, 2011 at 11:51 AM, Bas van Dijk <span dir="ltr">&lt;<a href="mailto:v.dijk.bas@gmail.com">v.dijk.bas@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello,<br>
<br>
I like to turn my Haskell program into a unix daemon. One of the steps<br>
in &quot;daemonizing&quot; a process is to fork it then exit the parent and<br>
continue with the child. All this is nicely abstracted in<br>
hdaemonize[1] which internally calls forkProcess[2].<br>
<br>
I would also like to use multiple simultaneous threads in my program.<br>
Unfortunately forkProcess is not supported when running with +RTS -N<br>
so I can&#39;t use hdaemonize.<br>
<br>
I understand why it&#39;s problematic to fork a process which is in the<br>
middle of running multiple simultaneous threads. However, in the case<br>
of a daemon the fork happens in the beginning of the program. So if I<br>
can manage to create a program that first daemonizes my process then<br>
starts the Haskell program, all is good.<br>
<br>
My current plan is to have a custom Haskell main function which is<br>
exported using the FFI:<br>
<br>
---------------------------------------------------------------------<br>
{-# LANGUAGE ForeignFunctionInterface #-}<br>
<br>
module MyMain where<br>
<br>
import Control.Monad       ( forM_ )<br>
import Control.Concurrent  ( threadDelay )<br>
<br>
-- from hsyslog:<br>
import System.Posix.Syslog ( Priority(Debug), syslog )<br>
<br>
foreign export ccall myMain :: IO ()<br>
<br>
myMain :: IO ()<br>
myMain = forM_ [1..10 :: Int] $ \n -&gt; do<br>
           syslog Debug $ &quot;test &quot; ++ show n<br>
           threadDelay 1000000<br>
---------------------------------------------------------------------<br>
<br>
Then create a C program that first daemonizes my process (using the<br>
&#39;daemon&#39;[3] function from unistd) then start up my custom Haskell main<br>
function:<br>
<br>
---------------------------------------------------------------------<br>
#include &lt;unistd.h&gt;<br>
#include &quot;HsFFI.h&quot;<br>
#include &quot;MyMain_stub.h&quot;<br>
<br>
extern void __stginit_Main ( void );<br>
<br>
int main(int argc, char *argv[])<br>
{<br>
  int r;<br>
  r = daemon(0,0);<br>
  if (r &lt; 0)<br>
  {<br>
    return r;<br>
  }<br>
<br>
  hs_init(&amp;argc, &amp;argv);<br>
  hs_add_root(__stginit_Main);<br>
  myMain();<br>
  hs_exit();<br>
  return 0;<br>
}<br>
---------------------------------------------------------------------<br>
<br>
My question is: how can I combine these two into a single program?<br>
<br>
I very much prefer to do this using Cabal since my actual program<br>
contains lots of dependencies.<br>
<br>
Thanks,<br>
<br>
Bas<br>
<br>
[1] <a href="http://hackage.haskell.org/package/hdaemonize" target="_blank">http://hackage.haskell.org/package/hdaemonize</a><br>
[2] <a href="http://hackage.haskell.org/packages/archive/unix/latest/doc/html/System-Posix-Process.html#v:forkProcess" target="_blank">http://hackage.haskell.org/packages/archive/unix/latest/doc/html/System-Posix-Process.html#v:forkProcess</a><br>

[3] <a href="http://www.kernel.org/doc/man-pages/online/pages/man3/daemon.3.html" target="_blank">http://www.kernel.org/doc/man-pages/online/pages/man3/daemon.3.html</a><br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br></div>