From: ara.t.howard@... Date: 2006-07-12T00:29:30+09:00 Subject: Re: Nuby Q: Daemonize (other materials have been read!) On Wed, 12 Jul 2006, Rogue Amateur wrote: > I've read the other references to daemonizing a process in ruby, but ... > well there's not enough to the examples to get me going properly. > > I'm using Ruby 1.8 on a Linux box. > > I am trying to write a function which will daemonize the current script > into one child process, which will then run isolated from the tty. > Using information found here (at the forum) and in other places, I wrote > this function: > > def daemonize() > # > # process documented in > http://blog.humlab.umu.se/samuel/archives/000107.html > # > begin > exit if fork > Process.setsid > exit if fork > File.umask 0774 > STDIN.reopen "/dev/null" # no stdin > STDOUT.reopen $log > STDERR.reopen STDOUT # stderr & stdout to syslog > rescue > raise "could not daemonize the process." > end > end # gvp_daemonize() > > Then I read in the 2nd Ed Pragmatic Ruby that there should be a > Process.detach, if I don't care about the parent getting feedback (which > I don't)., so I changed my first "exit if fork" to "if fork { > Process.detach\nexit }" > > The problem is that the process just exits. Nothing else gets done. I > know I'm doing something wrong, but I'm too much of a ruby-nuby (oh, > gee, and fork nuby too) to grok it from the examples I've seen, and the > book, and the web ... HELP please. here is a script which can be used on the command line to daemonize any other process: harp:~ > cat daemon.rb #! /usr//bin/env ruby fork{ STDIN.reopen(STDOUT.reopen(STDERR.reopen(open('/dev/null','r+')))); fork{ system ARGV.join(' ') ? exit : abort } and exit! } use it like harp:~ > daemon.rb any_other_command > FYI: I also need to figure out how to reset the userid and groupid, but > I have a book. :) you cannot do this from withint a script - a restriction placed by unix - only binary programs may be 'setuid' programs. i have a method to do this in ruby, which is often important for web and db related work where authentication might be allowed for only certain reasons. in any case setuid is really only appropriate for certain security situations and can be very, very bad for your system health. my inclination would be to say that if you are reading about it you shouldn't use it! ;-) that being said this will accomplish what you seek: http://codeforpeople.com/lib/ruby/_ruby/ regards. -a -- suffering increases your inner strength. also, the wishing for suffering makes the suffering disappear. - h.h. the 14th dali lama