shell

Limiting resources with limits (1)

limits is a FreeBSD’s base system utility for displaying and setting system resources limits. To some extent it is equivalent to the limit and ulimit commands. It can set limits for filesize, coredumpsize, maxproc, memoryuse and other parameters. Convenience modifiers like k, m or g for kilobytes, megabytes and gigabytes or s, m, h for seconds, minutes and hours can be used in all cases.

limits can be used to achieve three different goals:

  • Set resource limits.
    For example:

    $ limits -t 1s ls -R /

    launches the ls -R command with a cputime limit of 1 second. In general, this is the output that will be generated:

    ...
    ...
    ...
    Killed: 9

    If we want to know what happened, we can have a look at the system message buffer:

    $ dmesg
    pid 1651 (ls), uid 1001, was killed: exceeded maximum CPU limit
  • Show resource limits
    If we just want some information about resource limits, we can invoke the command this way:

    $ limits
    Resource limits (current):
      cputime                    infinity secs
      filesize                      infinity kB
      datasize                   33554432 kB
      stacksize                  524288 kB
      coredumpsize          infinity kB
      memoryuse              infinity kB
      memorylocked          infinity kB
      maxprocesses          5547
      openfiles                   11095
      sbsize                       infinity bytes
      vmemoryuse            infinity kB
      pseudo-terminals     infinity
      swapuse                   infinity kB

    Or if we are interested in one particular resource:

    $ limits -c
    Resource limits (current):
      coredumpsize         infinity kB 
  • Report resource limits in a way suitable for feeding a shell.
    This is pretty smart. Sometimes you want to set some limits on the invoking shell but simply calling limits will set the resource limit for the forked shell. If you don’t know what I am talking about, have a look at the fork-exec model for spawning processes in UNIX systems. limits transforms the resource limit command to the appropriate command for the invoking shell using either ulimit or limit depending on the shell. The limit command knows the following shells: sh, csh, bash, tcsh, ksh, pdksh and rc). Invoking limits with the -e flag results in the following output:

    $ limits -e -t 10s
    ulimit -t 10;

    It makes sense, since I am using bash. We can use this output together with the eval built-in command to apply the limit to the current shell:

    $ eval `limits -e -t 10s`
    $ limits -t
    Resource limits (current):
      cputime                    10 secs

That’s all for today.

Enjoy!

speak up

Add your comment below, or trackback from your own site.

Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*Required Fields