Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 467359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:34:22+00:00 2026-05-12T23:34:22+00:00

What is the proper way to create a well-behaved Unix or Linux daemon in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:34:22+00:00Added an answer on May 12, 2026 at 11:34 pm

    According to Stevens’s Advanced Programming in the UNIX Environment chapter 13, this is the procedure to make a well-behaved Unix daemon:

    1. Fork and have the parent exit. This makes the shell or boot script think the command is done. Also, the child process is guaranteed not to be a process group leader (a prerequisite for setsid next)
    2. Call setsid to create a new session. This does three things:
      1. The process becomes a session leader of a new session
      2. The process becomes the process group leader of a new process group
      3. The process has no controlling terminal
    3. Optionally fork again and have the parent exit. This guarantes that the daemon is not a session leader nor can it acquire a controlling terminal (under SVR4)
    4. Change the current working directory to / to avoid interfering with mounting and unmounting
    5. Set file mode creation mask to 000 to allow creation of files with any required permission later.
    6. Close unneeded file descriptors inherited from the parent (there is no controlling terminal anyway): stdout, stderr, and stdin.

    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.

    # Example double-forking Unix daemon initializer.
    
    raise 'Must run as root' if Process.euid != 0
    
    raise 'First fork failed' if (pid = fork) == -1
    exit unless pid.nil?
    
    Process.setsid
    raise 'Second fork failed' if (pid = fork) == -1
    exit unless pid.nil?
    puts "Daemon pid: #{Process.pid}" # Or save it somewhere, etc.
    
    Dir.chdir '/'
    File.umask 0000
    
    STDIN.reopen '/dev/null'
    STDOUT.reopen '/dev/null', 'a'
    STDERR.reopen STDOUT
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the proper way to create a hyperlink in Spring+JSP? There must be a
What's the proper way to convert from a scientific notation string such as 1.234567E-06
what is the proper way to scale an SDL Surface? I found one explanation
What is the most proper way to sending email of minimal 1000 or more
What is the proper way to minimize a WinForms app to the system tray?
What's proper way to connect ODBC datasources and execute some SQL statements? TQuery and
What is the proper way to terminate a Swing application from the code, and
what's the best/proper way of interacting between several windows in C# app? Recently, I've
What is the proper way to load a ListBox in C# .NET 2.0 Winforms?
What is the proper way to cast from an 'OLE_HANDLE' to an 'HICON' for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.