Stephen Reese

If you just want your program to simply run in the background, launch it with a “&” at the end of the command from the shell. However, if it expects to use stdout, stdin, or stderr, it will stop — so these must all be redirected to files or pipes.

This will still leave it attached to the terminal and process group of the shell, however. Thus you will not be able to log out of the command prompt with the background jobs unless you detach them. To get around this you can use the “nohup” and/or “setsid” commands when launching it.

If you want your program to daemonize itself (rather than relying on the user to do it when invoking it), then you will have to read some unix programming books about the steps involved. For example, Perl’s Proc::Daemon does the following:

  1. Fork a child and exit the parent process.

  2. Become a session leader (which detaches the program from the controlling terminal).

  3. Fork another child process and exit first child. This prevents the potential of acquiring a controlling terminal.

  4. Change the current working directory to “/”.

  5. Clear the file creation mask.

  6. Close all open file descriptors.


Comments

comments powered by Disqus