Going dark with the daemon
Do you think doing daemons is a complex task? Yes it is, unless you use a nice utility called daemon. The task of this program is to daemonize other commands or script in a simple and neat way. Does utility take any shortcuts? No, it just goes through all the steps we have already seen to detach a process from the controlling terminal, putting it in background, starting a new session, clearing the umask, and closing the old file descriptors. Well, doing it by ourselves in Bash coding will be quite a difficult task. This program makes everything straightforward, nothing to take care of manually. But there is a drawback: this is not a standard utility and must be installed by the user. Not a big issue indeed since many distributions such as Debian or Red Hat have a package for this utility.
Time to try this utility out, so let's take our write.sh
script and daemonize it:
root:# daemon -r /root/write.sh
We just called the daemon program, passing the full path to our...