# Modifying parent shell's attributes?

**URL:** <https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027>\
**Category:** Ruby\
**Created:** [December 13, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027 "2007-12-13T14:33:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [December 13, 2007, 2:33pm UTC](https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027/1 "2007-12-13T14:33:11Z")

</div>

I asked this on the rails list, but received no traction. Since it’s  
more of a ruby question anyway, thought I’d give it a shot here.

I have a problem with one of my rake tasks. It loads a very large yaml  
file into the db, but because of the default limitation on stack size  
in the Linux shell, the task craps out. Executing “ulimit -s 16384”  
fixes the issue, and the task completes just fine.

What I’d like to do within my task is this (in ruby-ish pseudo):

if on\_linux?  
set\_stack\_of\_running\_shell\_via\_ulimit 16384  
end

Is there any way to do this? Can I modify the attributes of the shell  
that is running me?

Thanks!  
John

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [December 13, 2007, 3:19pm UTC](https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027/2 "2007-12-13T15:19:47Z")

</div>

On 12/13/07, John W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> if on\_linux?  
> set\_stack\_of\_running\_shell\_via\_ulimit 16384  
> end
> 
> Is there any way to do this? Can I modify the attributes of the shell  
> that is running me?

Check out the Process class; specifically getrlimit and setrlimit. It  
won’t change the shell, but it will change Ruby.

-austin

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [December 13, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027/3 "2007-12-13T23:17:52Z")

</div>

On 12/13/07, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Check out the Process class; specifically getrlimit and setrlimit. It  
> > won’t change the shell, but it will change Ruby.
> > 
> > -austin
> 
> These are the little diamond things in ruby - bits of useful knowledge  
> 🙂

Yep…thanks very much austin!

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [January 2, 2008, 4:00am UTC](https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027/4 "2008-01-02T04:00:22Z")

</div>

On 12/13/07, Austin Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > if on\_linux?  
> > set\_stack\_of\_running\_shell\_via\_ulimit 16384  
> > end
> > 
> > Is there any way to do this? Can I modify the attributes of the shell  
> > that is running me?
> 
> Check out the Process class; specifically getrlimit and setrlimit. It  
> won’t change the shell, but it will change Ruby.

Following up a bit late on this, but this doesn’t solve my problem.  
for some reason.

I have tried:

Process.setrlimit Process::RLIMIT\_STACK, Process::RLIM\_INFINITY,  
Process::RLIM\_INFINITY  
Process.setrlimit Process::RLIMIT\_STACK, 0, Process::RLIM\_INFINITY

Is this the proper usage? It still craps out with same error, but  
setting ulimit in the calling shell to 16000 works just fine.

Thanks!  
John

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [December 13, 2007, 5:14pm UTC](https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027/5 "2007-12-13T17:14:30Z")

</div>

> Check out the Process class; specifically getrlimit and setrlimit. It  
> won’t change the shell, but it will change Ruby.
> 
> -austin

These are the little diamond things in ruby - bits of useful knowledge  
🙂

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [January 2, 2008, 4:20am UTC](https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027/6 "2008-01-02T04:20:48Z")

</div>

On Jan 1, 2008, at 9:59 PM, John W. wrote:

> I have tried:
> 
> Process.setrlimit Process::RLIMIT\_STACK, Process::RLIM\_INFINITY,  
> Process::RLIM\_INFINITY  
> Process.setrlimit Process::RLIMIT\_STACK, 0, Process::RLIM\_INFINITY

It looks like you are trying to increase the max stack size  
to RLIM\_INFINITY, which can only be done if you have super-user  
access. What you want to do is to just raise the soft-limit. Try the  
following in IRB and see if it works on your system:

> > include Process  
> > =\> Object
> 
> > s = getrlimit RLIMIT\_STACK  
> > =\> [8388608, 67108864]
> 
> > setrlimit RLIMIT\_STACK, s.first\*2, s.last  
> > =\> nil
> 
> > getrlimit RLIMIT\_STACK  
> > =\> [16777216, 67108864]
> 
> >

In this example, I’m just changing the stack limit to twice its  
default size, which is still less than the max limit of 67108864.

This example was done on a Mac OS 10.4 system.

Gary W.

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [January 2, 2008, 4:52am UTC](https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027/7 "2008-01-02T04:52:31Z")

</div>

On Jan 1, 2008, at 10:38 PM, John W. wrote:

> I’m on Linux, and I’ve even done this:
> 
> Process.setrlimit Process::RLIMIT\_STACK, 18446744073709551615,  
> 18446744073709551615
> 
> And, based on what getrlimit says, it worked. However, same exception.
> 
> Even doing it as root, nothing else seems to work but ulimit -s 16000  
> from parent shell.

I’d avoid huge numbers like that–you may be causing other problems  
because you are overcommitting memory. Just try using the same value  
you have had success with via ulimit: 16384000 (in bytes).

How about calling Process.setrlimit in Ruby and then forking to  
run your actually code?

Process.setrlimit Process::RLIMIT\_STACK, new\_limit, new\_max

child = fork { do\_work }  
Process.wait(child)

Gary W.

---

<div class="post-metadata">

**Author:** ![princenamor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/c0e974/32.png) [@princenamor](https://www.ruby-forum.com/u/princenamor)\
**Post date:** [January 2, 2008, 4:39am UTC](https://www.ruby-forum.com/t/modifying-parent-shells-attributes/123027/8 "2008-01-02T04:39:03Z")

</div>

On 1/1/08, Gary W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It looks like you are trying to increase the max stack size
> 
> > > getrlimit RLIMIT\_STACK  
> > > =\> [16777216, 67108864]

I’m on Linux, and I’ve even done this:

Process.setrlimit Process::RLIMIT\_STACK, 18446744073709551615,  
18446744073709551615

And, based on what getrlimit says, it worked. However, same exception.

Even doing it as root, nothing else seems to work but ulimit -s 16000  
from parent shell.

ruby 1.8.6 (2007-06-07 patchlevel 36) [i486-linux]
