Checking limits of a running Linux process – ulimit

If we are dealing with servers that need to handle a lot of concurrent connections, we soon bump limits of our default Linux server installation. In order to be able to further modify these limits, Linux has ulimit. We can set system wide limits on CentOS in /etc/security/limits.conf or manually setting them with ulimit command. We can view our current limits of logged in user by simply typing ulimit -a which outputs something similar to:

mongoserver041 ~ $ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 125029
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1000000
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 32000
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited

These are our modified MongoDB server’s limits, you might have different. These limits go for root user, since we executed this command as root. Specific user and process might have different limits, depending on your limits.conf configuration. But how do we check whether we actually have these limits on our current running mongo process? For this purpose Linux doesn’t have a command and we need to check that manually in our /proc/pid/limits. To make things easier I found this script and modified it a bit for my needs:

#!/bin/bash
if [ "$#" -ne "1" ]; then
   echo ""
   echo -e "\033[01;32mLimit checker\033[00m"
   echo -e "\033[01;37mUsage:\033[01;33m $0 process_name\033[00m"
   echo ""
   exit 0
fi

return-limits(){
   for process in $@; do
      process_pids=`ps -C $process -o pid --no-headers | cut -d " " -f 2`

      if [ -z $@ ]; then
         echo "[no $process running]"
      else
         for pid in $process_pids; do
            echo "[$process #$pid -- limits]"
            cat /proc/$pid/limits
      done
      fi
   done
}

return-limits $1

Output of the script looks like this:

mongoserver041 ~ $ ./limits.sh mongod
[mongod #43447 -- limits]
Limit                     Soft Limit           Hard Limit           Units
Max cpu time              unlimited            unlimited            seconds
Max file size             unlimited            unlimited            bytes
Max data size             unlimited            unlimited            bytes
Max stack size            10485760             unlimited            bytes
Max core file size        0                    unlimited            bytes
Max resident set          unlimited            unlimited            bytes
Max processes             32000                32000                processes
Max open files            1000000              1000000              files
Max locked memory         65536                unlimited            bytes
Max address space         unlimited            unlimited            bytes
Max file locks            unlimited            unlimited            locks
Max pending signals       125029               125029               signals
Max msgqueue size         819200               819200               bytes
Max nice priority         0                    0
Max realtime priority     0                    0
Max realtime timeout      unlimited            unlimited            us

Leave a Reply

help-hint.png
Purpose of the commenting system is to share your experience. I encourage you to post feedback with your own suggestions, ideas or optimizations regarding the topic of a blog post. What commenting system isn't for, is asking questions about similar issues of yours and requesting support for it. Blog post is provided as is and I am not here to solve all your problems. Please bear that in mind and try to avoid posting such comments. I do take privilege to remove comment from my blog for any reason whatsoever. Usually I do it when I sense a comment was posted only for spam/seo reasons or is out of blog post's topic. Thank you for reading this, now you may continue :)
 

Your email address will not be published. Required fields are marked *