Trying to create a Ruby daemon

Hi,
Does anyone here have an example on how to create a Ruby daemon that
runs as a session leader (without a controlling terminal)? I’ve tried
to accomplish that using fork and Process.setpgrp, but the only results
so far have been zombie processes or processes that exit when I close
the terminal. I’m quite new to Ruby so I guess that I’ve missed some
little detail somewhere… :slight_smile:

cheers,
//samuel kvarnbrink

Hi,
Does anyone here have an example on how to create a Ruby daemon that
runs as a session leader (without a controlling terminal)? I’ve tried

As long as I’ve been using Linux, I’m embarraced to admit that I still
draw a blank on process groups and shell sessions…

to accomplish that using fork and Process.setpgrp, but the only results
so far have been zombie processes or processes that exit when I close
the terminal. I’m quite new to Ruby so I guess that I’ve missed some
little detail somewhere… :slight_smile:

Well, I’ve done a few, so I figured I’d write a class for proper
‘daemonization’. I’ll optionally create a pid file and a socket if you
want that. And as I’ve had some problems with crashes, it can fork
twice, having the first one watch over the next and reforking if it
exits with an error (or crashes). I should clean it up and post it on
RAA, but haven’t quite gotten there yet. Give a shout if you’d like to
try it.

···

On Sun, Dec 07, 2003 at 09:06:17PM +0900, Samuel Kvarnbrink wrote:


Thomas
beast@system-tnt.dk

Daemonization in seven easy steps [1]:

exit if fork # Parent exits, child continues.
Process.setsid # Become session leader.
exit if fork # Zap session leader. See [1].
Dir.chdir “/” # Release old working directory.
File.umask 0000 # Ensure sensible umask. Adjust as needed.
STDIN.reopen “/dev/null” # Free file descriptors and
STDOUT.reopen “/dev/null”, “a” # point them somewhere sensible.
STDERR.reopen STDOUT # STDOUT/ERR should better go to a logfile.

Refine as necessary.

		Reimer Behrends

[1] http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16

···

Samuel Kvarnbrink (samuel.kvarnbrink@humlab.umu.se) wrote:

Hi,
Does anyone here have an example on how to create a Ruby daemon that
runs as a session leader (without a controlling terminal)? I’ve tried
to accomplish that using fork and Process.setpgrp, but the only results
so far have been zombie processes or processes that exit when I close
the terminal. I’m quite new to Ruby so I guess that I’ve missed some
little detail somewhere… :slight_smile:

In Message-Id: D58DEDEE-28AD-11D8-A981-000393DA3608@humlab.umu.se
Samuel Kvarnbrink samuel.kvarnbrink@humlab.umu.se writes:

Does anyone here have an example on how to create a Ruby daemon that
runs as a session leader (without a controlling terminal)? I’ve tried
to accomplish that using fork and Process.setpgrp, but the only
results so far have been zombie processes or processes that exit when
I close the terminal. I’m quite new to Ruby so I guess that I’ve
missed some little detail somewhere… :slight_smile:

Following is a tiny sample. You can get more knowledge from other
daemon codes and man pages. A book like “Advanced Programming in the
UNIX Environment” also helps a lot.

See Also: daemon(3), setsid(2), setpgrp(2), wait(2), ps(1) and

tty(4) if needed (on BSD based system, it may contains info around

TIOCNOTTY).

# parent process exits to pass child to init(1).
fork and exit

# child becomes session leader and disassociates controlling tty.
# namely do Process.setpgrp + \alpha.
Process.setsid

# at here already the child process have become daemon. the rest
# is just for behaving well.

# ensure no extra I/O.
File.open("/dev/null", "r+") do
  >devnull>
  $stdin.reopen(devnull)
  $stdout.reopen(devnull)
  $stderr.reopen(devnull)
end

# ensure daemon process not to prevent shutdown process.
Dir.chdir("/")
···


kjana@dm4lab.to December 7, 2003
Art is long, and time is fleeting.

As long as I’ve been using Linux, I’m embarraced to admit that I still
draw a blank on process groups and shell sessions…

Same here :slight_smile:

Well, I’ve done a few, so I figured I’d write a class for proper
‘daemonization’. I’ll optionally create a pid file and a socket if you
want that. And as I’ve had some problems with crashes, it can fork
twice, having the first one watch over the next and reforking if it
exits with an error (or crashes). I should clean it up and post it on
RAA, but haven’t quite gotten there yet. Give a shout if you’d like to
try it.

That would be great - I’d love to try it! A “daemonization”
class/module (like Proc::Daemon in Perl) is one of the (very few)
things I’ve been missing in Ruby.

//samuel

Great! Now my daemon works just the way I want it to :slight_smile:

Thanks a lot to everybody for all the useful help!

//samuel

2003-12-07 kl. 14.52 skrev Reimer Behrends:

···

Daemonization in seven easy steps [1]:

exit if fork # Parent exits, child continues.
Process.setsid # Become session leader.
exit if fork # Zap session leader. See [1].
Dir.chdir “/” # Release old working directory.
File.umask 0000 # Ensure sensible umask. Adjust as
needed.
STDIN.reopen “/dev/null” # Free file descriptors and
STDOUT.reopen “/dev/null”, “a” # point them somewhere sensible.
STDERR.reopen STDOUT # STDOUT/ERR should better go to a
logfile.

Refine as necessary.

  	Reimer Behrends

[1] http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16

You really need a second fork here, as the previous post suggested, if
you do not want to get into trouble with unexpectedly acquiring a
controlling terminal. Generally in Unix (with some exceptions), the
first tty device opened by a session/group leader (unless opened with
O_NOCTTY) becomes the controlling terminal for the whole session/group.

Gennady.

···

On Dec 7, 2003, at 6:37, YANAGAWA Kazuhisa wrote:

In Message-Id: D58DEDEE-28AD-11D8-A981-000393DA3608@humlab.umu.se
Samuel Kvarnbrink samuel.kvarnbrink@humlab.umu.se writes:

Does anyone here have an example on how to create a Ruby daemon that
runs as a session leader (without a controlling terminal)? I’ve tried
to accomplish that using fork and Process.setpgrp, but the only
results so far have been zombie processes or processes that exit when
I close the terminal. I’m quite new to Ruby so I guess that I’ve
missed some little detail somewhere… :slight_smile:

Following is a tiny sample. You can get more knowledge from other
daemon codes and man pages. A book like “Advanced Programming in the
UNIX Environment” also helps a lot.

See Also: daemon(3), setsid(2), setpgrp(2), wait(2), ps(1) and

tty(4) if needed (on BSD based system, it may contains info around

TIOCNOTTY).

# parent process exits to pass child to init(1).
fork and exit

# child becomes session leader and disassociates controlling tty.
# namely do Process.setpgrp + \alpha.
Process.setsid

# at here already the child process have become daemon. the rest
# is just for behaving well.

# ensure no extra I/O.
File.open("/dev/null", "r+") do
  >devnull>
  $stdin.reopen(devnull)
  $stdout.reopen(devnull)
  $stderr.reopen(devnull)
end

# ensure daemon process not to prevent shutdown process.
Dir.chdir("/")


kjana@dm4lab.to December 7, 2003
Art is long, and time is fleeting.

[courtesy cc of this posting sent to cited author via email]

In article 4CAB270F-28BC-11D8-A981-000393DA3608@humlab.umu.se,

···

Samuel Kvarnbrink samuel.kvarnbrink@humlab.umu.se wrote:

That would be great - I’d love to try it! A “daemonization”
class/module (like Proc::Daemon in Perl) is one of the (very few)
things I’ve been missing in Ruby.

How about using Process.setsid?

Ollivier ROBERT -=- Eurocontrol EEC/AMI -=- roberto@eurocontrol.fr
Usenet Canal Historique FreeBSD: The Power to Serve!

As both Gennady and Reimer already pointed out, using Process.setsid
isn’t enough if you want a “clean” daemonization. I was thinking more of
a 100% “idiot-proof” class that handles all the necessary steps by
itself. Of course, that’s not a major problem to me anymore (now that I
know how to do it) but I think it would be a lot more newbie-friendly to
have such a class.

cheers
//samuel

···

On Wed, 2003-12-10 at 14:22, Ollivier Robert wrote:

[courtesy cc of this posting sent to cited author via email]

In article 4CAB270F-28BC-11D8-A981-000393DA3608@humlab.umu.se,
Samuel Kvarnbrink samuel.kvarnbrink@humlab.umu.se wrote:

That would be great - I’d love to try it! A “daemonization”
class/module (like Proc::Daemon in Perl) is one of the (very few)
things I’ve been missing in Ruby.

How about using Process.setsid?

“Samuel Kvarnbrink” samuel.kvarnbrink@humlab.umu.se schrieb im
Newsbeitrag news:1071064667.513.11.camel@humpc7…

···

On Wed, 2003-12-10 at 14:22, Ollivier Robert wrote:

[courtesy cc of this posting sent to cited author via email]

In article 4CAB270F-28BC-11D8-A981-000393DA3608@humlab.umu.se,
Samuel Kvarnbrink samuel.kvarnbrink@humlab.umu.se wrote:

That would be great - I’d love to try it! A “daemonization”
class/module (like Proc::Daemon in Perl) is one of the (very few)
things I’ve been missing in Ruby.

How about using Process.setsid?

As both Gennady and Reimer already pointed out, using Process.setsid
isn’t enough if you want a “clean” daemonization. I was thinking more of
a 100% “idiot-proof” class that handles all the necessary steps by
itself. Of course, that’s not a major problem to me anymore (now that I
know how to do it) but I think it would be a lot more newbie-friendly to
have such a class.

Why then not put your newly gained knowledge into a module / class and put
it into the RAA?
:slight_smile:

Regards

robert

Why then not put your newly gained knowledge into a module / class and
put
it into the RAA?
:slight_smile:

Ask me again when my degree project is finished… deadline
approaching! :slight_smile:

//samuel