7 Replies
      Latest reply on Feb 1, 2019 1:52 AM by eskimo
      akram.halder Level 1 Level 1 (0 points)

        II use daemon(1,0) to demonize the process. But Daemon() API is deprecated long back.


        So tried achieving demonize through fork(),  but service is getting closed when I close the parent process. What should be the issue?

        Code snippet is added  below.

        Please suggest any thing is wrong in below code.


        bool daemon_service::daemon_service_impl::daemonise()


        int ret = fork();

        if (ret == -1)


          // fork() failed.

        last_error_ = last_system_error();

        return false;



        if (ret != 0)


        // This is the parent non-daemon.




        // This must be the child daemon.

        is_daemon_ = true;



          // detach from the terminal



        // Close fd's 0,1,2. Needed if started by rsh


        close(0); close(1); close(2);


        return true;


        • Re: Killing parent process closing the child process and the application in Mojave
          eskimo Apple Staff Apple Staff (11,625 points)

          I use daemon(1,0) to demonize the process.

          There are fundamental problems with this approach on macOS, which is why daemon was deprecated.  Specifically, macOS processes have more execution context than the traditional UNIX execution context.  daemon only switches the traditional UNIX execution context, and thus your process ends up in a weird state where half of its context has been daemonised and the other half hasn’t.

          You can learn more about the general background to this in Technote 2083 Daemons and Agents.

          The only reliable way to run code as a daemon is to have it originally launched in the daemon context.  However, whether this is the right solution or not depends on your goals, and specifically whether you want to create a daemon (that’s global to the system as a whole) or agent (a background process that’s tied to a specific login session).

          If you explain more about the background to your program — how it gets launched and how long you want it to stay running — I should be able to offer you more concrete advice.

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"