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:
-
Fork a child and exit the parent process.
-
Become a session leader (which detaches the program from the controlling terminal).
-
Fork another child process and exit first child. This prevents the potential of acquiring a controlling terminal.
-
Change the current working directory to “/”.
-
Clear the file creation mask.
-
Close all open file descriptors.
Comments
comments powered by Disqus