What is the proper way to create a well-behaved Unix or Linux daemon in Ruby?
What is the definition of a well-behaved daemon anyway, and how would one write such a program in Ruby?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
According to Stevens’s Advanced Programming in the UNIX Environment chapter 13, this is the procedure to make a well-behaved Unix daemon:
setsidto create a new session. This does three things:/to avoid interfering with mounting and unmountingstdout,stderr, andstdin.Nowadays there is a file to track the PID which is used heavily by Linux distribution boot scripts. Be sure to write out the PID of the grandchild, either the return value of the second fork (step 3) or the value of
getpid()after step 3.Here is a Ruby implementation, mostly translated from the book, but with the double-fork and writing out the daemon PID.