'Daemonizing' a script

C

Carl Youngblood

I got this code from Webrick::Daemon that is supposed to turn a ruby
script into a daemon. Though I don't fully understand it, I guess that
means that it detaches it from the terminal that called it. The thing
is, when I run the script without the daemonizing code it works fine,
but when I add the following code it silently croaks. Does anybody know
what might be going wrong?

Thanks,
Carl Youngblood

#------------------------------

# the following lines "daemonize" this script
exit!(0) if fork
Process::setsid
exit!(0) if fork
Dir::chdir("/")
File::umask(0)
[ STDIN, STDOUT, STDERR ].each{|io|
io.reopen("/dev/null", "r+")
}

# This is a session storage daemon which can be shared by multiple FCGI
# processes. It's just a hash which is enabled for DRb access.

require 'drb'
require 'include/localsettings'

session_data = Hash.new
DRb.start_service("druby://127.0.0.1:#{SESSION_PORT.to_s}", session_data)
DRb.thread.join
 
C

Carl Youngblood

Never mind. I feel sheepish for bothering the list with this. I just
needed to turn off error output to /dev/null to see what was going on.
Sorry. :-(
 
G

Gennady

What do you mean by "silently croaks"? Like nothing happens, just a
prompt? Did you check your processes? As a result of this code there
must be a detached process with parent id 1 that do not have
controlling terminal. Try:

ps -ef |grep ruby

and scan for a corresponding process (it is Unix, isn't it? It may not
work on Windows).

I got this code from Webrick::Daemon that is supposed to turn a ruby
script into a daemon. Though I don't fully understand it, I guess
that means that it detaches it from the terminal that called it. The
thing is, when I run the script without the daemonizing code it works
fine, but when I add the following code it silently croaks. Does
anybody know what might be going wrong?

Thanks,
Carl Youngblood

#------------------------------

# the following lines "daemonize" this script
exit!(0) if fork
Process::setsid
exit!(0) if fork
Dir::chdir("/")
File::umask(0)
[ STDIN, STDOUT, STDERR ].each{|io|
io.reopen("/dev/null", "r+")
}

# This is a session storage daemon which can be shared by multiple FCGI
# processes. It's just a hash which is enabled for DRb access.

require 'drb'
require 'include/localsettings'

session_data = Hash.new
DRb.start_service("druby://127.0.0.1:#{SESSION_PORT.to_s}",
session_data)
DRb.thread.join

Sincerely,
Gennady Bystritsky
 
H

Hal Fulton

Carl said:
Never mind. I feel sheepish for bothering the list with this. I just
needed to turn off error output to /dev/null to see what was going on.
Sorry. :-(

Ha, no sweat, Carl. We've all done it.

Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top