2003/12/30 Understanding ulimit

This sets limits on processes. It's well known, but I suspect many people only know it for setting the size of core files. It can actually control much more :


$ ulimit -a
core file size        (blocks, -c) unlimited
data seg size         (kbytes, -d) 524288
file size             (blocks, -f) unlimited
max locked memory     (kbytes, -l) unlimited
max memory size       (kbytes, -m) unlimited
open files                    (-n) 16424
pipe size          (512 bytes, -p) 1
stack size            (kbytes, -s) 65536
cpu time             (seconds, -t) unlimited
max user processes            (-u) 8211
virtual memory        (kbytes, -v) 589824
 

You can use it to control processes that you expect to end but might "run away" or even to put a stop to things you deliberately have put in a "forever" loop. For example, this script would run forever if it were not for the beginning line:

ulimit -t 1
while :
do
ps -lp $$
done
 

But in fact it will be stopped:

  UID   PID  PPID CPU PRI NI      VSZ    RSS WCHAN  STAT  TT       TIME COMMAND
  501  2714   661   0  28  0    18644    396 -      R+   std    0:01.30 /bin/ba
  UID   PID  PPID CPU PRI NI      VSZ    RSS WCHAN  STAT  TT       TIME COMMAND
  501  2714   661   0  28  0    18644    396 -      R+   std    0:01.30 /bin/ba
  UID   PID  PPID CPU PRI NI      VSZ    RSS WCHAN  STAT  TT       TIME COMMAND
  501  2714   661   0  29  0    18644    396 -      R+   std    0:01.30 /bin/ba
  UID   PID  PPID CPU PRI NI      VSZ    RSS WCHAN  STAT  TT       TIME COMMAND
  501  2714   661   0  29  0    18644    396 -      R+   std    0:01.30 /bin/ba
Cputime limit exceeded
$ 
 

Things to note: it doesn't stop after exactly 1 second of time, and that is cpu time, not wall time. And, this is a function of your shell, so you need to do "man bash" etc. to find out what its capabilities are.

You can do similar things to control badly written processes that get confused and start sucking down too much memory or have other bad behavior. Just start them from a script that sets appropriate limits.

The system call is "setrlimit".



Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Understanding ulimit




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Tony Lawrence



Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us





Keeping URIs so that they will still be around in 2, 20 or 200 or even 2000 years is clearly not as simple as it sounds ... However, all over the Web, webmasters are making decisions which will make it really difficult for themselves in the future. (Tim Berners-Lee)

The camel has evolved to be relatively self-sufficient. (On the other hand, the camel has not evolved to smell good. Neither has Perl.) (Larry Wall)







This post tagged: