[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Nuby Q: Daemonize (other materials have been read!

Rogue Amateur

7/11/2006 3:17:00 PM

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/0...
#
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.

FYI: I also need to figure out how to reset the userid and groupid, but
I have a book. :)

Thanks for any assistance rendered.

Rogue Amateur

--
Posted via http://www.ruby-....

14 Answers

Ara.T.Howard

7/11/2006 3:30:00 PM

0

Rogue Amateur

7/11/2006 4:21:00 PM

0

First, let me say thank you for the help.

unknown wrote:
> On Wed, 12 Jul 2006, Rogue Amateur wrote:
>
[snip]
>
> 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

Ok, ignorance again:
1) this means I can't do the fork within the script itself?
2) suppose the any_other_command isn't in the default ruby library? do
I supply the full path to call? Will it allow me to pass along
arguments?
(I'll be trying it, as I await a response...)

>
>> 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

[snip]

> my inclination would be to say that if you are reading
> about it
> you shouldn't use it! ;-)

Gotcha. So the follow on question is: if I need my now isolated process
to run as a particular user/group, the process is...? What? My local
guru, who speaks about 15 levels too high for me, said "set the user and
group ids." See, I can parrot! :)

I'm familiar with sudo, but... would that work with the daemon.rb? (eg:
sudo daemon.rb My_Ruby_Script.rb [arg] [arg]...)

Thanks again
Rogue Amateur

--
Posted via http://www.ruby-....

Rogue Amateur

7/11/2006 4:29:00 PM

0

Rogue Amateur wrote:
[snip, in follow up]

> Ok, ignorance again:
> 1) this means I can't do the fork within the script itself?
> 2) suppose the any_other_command isn't in the default ruby library? do
> I supply the full path to call? Will it allow me to pass along
> arguments?
> (I'll be trying it, as I await a response...)
>
[snip again]

I tried it:
/daemon.rb ./my_Script.rb
it dies without comment.

running ./my_Script.rb, however, works as expected. Notions?

RA

--
Posted via http://www.ruby-....

Ara.T.Howard

7/11/2006 4:35:00 PM

0

Rogue Amateur

7/11/2006 4:46:00 PM

0

unknown wrote:
> On Wed, 12 Jul 2006, Rogue Amateur wrote:
>
>> [snip again]
>>
>> I tried it:
>> ./daemon.rb ./my_Script.rb
>> it dies without comment.
>>
>> running ./my_Script.rb, however, works as expected. Notions?
>
> it doesn't die - it starts the script as a daemon. check in top.
>



Ok, then I'm really confused. Because the script is NOT running.

The script in question sends data to and receives data from a socket. I
have test code written which sends data to the appropriate socket, and
receives back, and prints the result.

When I started the listener, then daemonized the script, the listener
sat there doing nothing. ps -ef returned nothing (for the script name).
When I started the script alone, suddenly the listener has something to
do, and the ps -ef returns data. I am sure I did something wrong, I
don't doubt it a bit, but... it really doesn't look like the script is
running as a daemon. Am I using the wrong checks? Will my sockets
suddenly work differently when run as a daemon? I haven't found
anything that says that's likely so... I must be confused.

Thank you for your quick responses.

RA

--
Posted via http://www.ruby-....

Andrew RJ

7/11/2006 5:02:00 PM

0


Why don't you use the daemonize ruby library? It works great on
linux/solaris.

Check out the 4 liner over at redhanded:

http://redhanded.hobix.com/inspect/daem...

Need the library?

http://grub.ath.cx/...

ARJ

--
Posted via http://www.ruby-....

Ara.T.Howard

7/11/2006 5:21:00 PM

0

Rogue Amateur

7/13/2006 3:10:00 PM

0

unknown wrote:
> On Wed, 12 Jul 2006, Rogue Amateur wrote:
>
>>>
>>

[snip]

> fortytwo :~ > daemon sleep 42
>

So, this works. So does another, innocuous script I wrote. I even
checked out the daemonize library the other person suggested, and it
works too. My pain in the butt actually need it to work as a daemon,
however, does not work.

What could cause code which works in interactive mode to fail in daemon
mode?

I'm checking the code now, and I do have "if debug" statements which
output to STDOUT. Could that be the problem?

I'm adding some blurbs to try to get the stupid thing to tell me, but it
seems to die so silently (eg: nothing in syslog, which is where most
messages are expected to go), that I can't get enough information.

I know it manages to send the "shut down the listener" message, which is
a "rescue" action.

Hints, suggestions and other comments?

RA

--
Posted via http://www.ruby-....

Berger, Daniel

7/13/2006 3:16:00 PM

0

Rogue Amateur wrote:
> unknown wrote:
>> On Wed, 12 Jul 2006, Rogue Amateur wrote:
>>
>
> [snip]
>
>> fortytwo :~ > daemon sleep 42
>>
>
> So, this works. So does another, innocuous script I wrote. I even
> checked out the daemonize library the other person suggested, and it
> works too. My pain in the butt actually need it to work as a daemon,
> however, does not work.
>
> What could cause code which works in interactive mode to fail in daemon
> mode?
>
> I'm checking the code now, and I do have "if debug" statements which
> output to STDOUT. Could that be the problem?

I'm guessing that the daemon library calls setsid() behind the scenes, in which
case there's no controlling terminal for that process. I would think sending
anything to STDOUT would just disappear into the ether, but I'm not positive.

Regards,

Dan


This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly
prohibited and may be unlawful. If you have received this communication
in error, please immediately notify the sender by reply e-mail and destroy
all copies of the communication and any attachments.

Ara.T.Howard

7/13/2006 3:31:00 PM

0