# Re: forking processes and writing to log file

**URL:** <https://www.ruby-forum.com/t/re-forking-processes-and-writing-to-log-file/70124>\
**Category:** Ruby\
**Created:** [September 26, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/re-forking-processes-and-writing-to-log-file/70124 "2006-09-26T23:32:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [September 26, 2006, 11:32pm UTC](https://www.ruby-forum.com/t/re-forking-processes-and-writing-to-log-file/70124/1 "2006-09-26T23:32:06Z")

</div>

Joe Van D. wrote:

> On 9/26/06, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hello,
> > 
> > So, on linux, I’m forking a new Ruby process. I redirect stdout and  
> > stderr into a file and then I execute another program via ‘system’.
> 
> I should probably mention that the other program that’s being executed  
> is rsh, which executes a command on a different machine.

You either did not redirect stdout/stderr to a file properly, or  
something you execute tries to output directly to the controlling  
terminal (/dev/tty). You can disassosiate your spawned process from a  
controlling terminal, just to be sure (not with ‘system’ though):

logfile = “my\_log\_file.log”  
pid = fork do  
File.open(logfile, ‘w’) do |\_io|  
$stdout.reopen(\_io)  
$stderr.reopen(\_io)  
end

Process.setsess # In this scenario disassociates the controlling  
# terminal until another tty device is open (will  
# become new controlling tty).

exec “the\_command\_line\_used\_to\_be\_launched\_by\_system”  
end

Process.waitpid pid

===  
Gennady.

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [September 27, 2006, 12:04am UTC](https://www.ruby-forum.com/t/re-forking-processes-and-writing-to-log-file/70124/2 "2006-09-27T00:04:10Z")

</div>

On 9/26/06, Gennady B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You either did not redirect stdout/stderr to a file properly, or
> 
> Process.setsess # In this scenario disassociates the controlling  
> # terminal until another tty device is open (will  
> # become new controlling tty).
> 
> exec “the\_command\_line\_used\_to\_be\_launched\_by\_system”  
> end
> 
> Process.waitpid pid

I assume you mean Process.setsid?

I have this function called at the beginning of the fork:

# Redirect stdout and stderr to a file and flush the buffers on a write

def redirect\_all\_output\_to\_file file  
log = File.new(file, “w+”)  
STDOUT.reopen log; STDERR.reopen log  
STDOUT.sync = true  
end

When I add Process.setsid to the end of that function, when I  
background the ruby process, stuff keeps writing to the log file, but  
my controlling terminal gets all messed up. The only keys on my  
keyboard that work are ‘e’, ‘s’, the space bar, and the backspace key,  
and they only work half the time. The terminal works normally once  
the ruby process is killed.

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [September 27, 2006, 12:30am UTC](https://www.ruby-forum.com/t/re-forking-processes-and-writing-to-log-file/70124/3 "2006-09-27T00:30:12Z")

</div>

On 9/26/06, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > I assume you mean Process.setsid?  
> > background the ruby process, stuff keeps writing to the log file, but  
> > my controlling terminal gets all messed up. The only keys on my  
> > keyboard that work are ‘e’, ‘s’, the space bar, and the backspace key,  
> > and they only work half the time. The terminal works normally once  
> > the ruby process is killed.

Ah, this appears to be an rsh issue, not a Ruby one. (can’t use ssh  
here because people are too lazy to set up ssh keys or something)

$ rsh machine some\_command &  
That seems to need to wait for input, even if some\_command doesn’t  
read from stdin.

$ ssh machine some\_command &  
works as expected.

My workaround is to create a file with half a dozen blank lines and do  
$ rsh machine some\_command \< file\_with\_blank\_lines &

But that seems icky.

---

<div class="post-metadata">

**Author:** ![Gennady\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c57346/32.png) [@Gennady\_B](https://www.ruby-forum.com/u/Gennady_B)\
**Post date:** [September 27, 2006, 12:34am UTC](https://www.ruby-forum.com/t/re-forking-processes-and-writing-to-log-file/70124/4 "2006-09-27T00:34:12Z")

</div>

On 9/26/06, Joe Van D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > is rsh, which executes a command on a different machine.  
> > > > $stderr.reopen(\_io)
> > 
> > When I add Process.setsid to the end of that function, when I  
> > That seems to need to wait for input, even if some\_command doesn’t  
> > read from stdin.
> 
> $ ssh machine some\_command &  
> works as expected.
> 
> My workaround is to create a file with half a dozen blank lines and do  
> $ rsh machine some\_command \< file\_with\_blank\_lines &
> 
> But that seems icky.

Sorry for all the non-ruby related noise, but the problem is fixed.  
From the BUGS section in the rsh man page:

```
 If you are using csh(1) and put a rsh in the background without 

```

redirect-  
ing its input away from the terminal, it will block even if no  
reads are  
posted by the remote command. If no input is desired you should  
redirect  
the input of rsh to /dev/null using the -n option.

Joe
