APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

2003/12/30 Understanding ulimit


© December 2003 Tony Lawrence

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


Inexpensive and informative Apple related e-books:

Take Control of the Mac Command Line with Terminal, Second Edition

Are Your Bits Flipped?

Take Control of Pages

Photos for Mac: A Take Control Crash Course

Take Control of Parallels Desktop 12




More Articles by © Tony Lawrence




Printer Friendly Version

Have you tried Searching this site?

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


Printer Friendly Version





Technology is a word that describes something that doesn’t work yet. (Douglas Adams)




Linux posts

Troubleshooting posts


This post tagged:

Kernel

Programming

Scripting

Shell

UnixWords



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode





SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)
www.SCOsales.com