{"id":5555,"date":"2016-01-07T13:38:05","date_gmt":"2016-01-07T05:38:05","guid":{"rendered":"http:\/\/rmohan.com\/?p=5555"},"modified":"2016-01-07T13:38:05","modified_gmt":"2016-01-07T05:38:05","slug":"how-to-find-out-which-processes-are-swapping-in-linux","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=5555","title":{"rendered":"how to find out which processes are swapping in linux"},"content":{"rendered":"<p>Have you ever logged in to a server, ran free, seen that a bit of swap is used and wondered what\u2019s in there? It\u2019s usually not very indicative of anything, or even overly helpful knowing what\u2019s in there, mostly it\u2019s a curiosity thing.<\/p>\n<p>Either way, starting from kernel 2.6.16, we can find out using smaps which can be found in the proc filesystem. I\u2019ve written a simple bash script which prints out all running processes and their swap usage. It\u2019s quick and dirty, but does the job and can easily be modified to work on any info exposed in \/proc\/$PID\/smaps If I find the time and inspiration, I might tidy it up and extend it a bit to cover some more alternatives. The output is in kilobytes.<\/p>\n<p>#!\/bin\/bash<br \/>\n# Get current swap usage for all running processes<br \/>\n# Erik Ljungstrom 27\/05\/2011<br \/>\nSUM=0<br \/>\nOVERALL=0<br \/>\nfor DIR in `find \/proc\/ -maxdepth 1 -type d | egrep &#8220;^\/proc\/[0-9]&#8221;` ; do<br \/>\nPID=`echo $DIR | cut -d \/ -f 3`<br \/>\nPROGNAME=`ps -p $PID -o comm &#8211;no-headers`<br \/>\nfor SWAP in `grep Swap $DIR\/smaps 2&gt;\/dev\/null| awk &#8216;{ print $2 }&#8217;`<br \/>\ndo<br \/>\nlet SUM=$SUM+$SWAP<br \/>\ndone<br \/>\necho &#8220;PID=$PID &#8211; Swap used: $SUM &#8211; ($PROGNAME )&#8221;<br \/>\nlet OVERALL=$OVERALL+$SUM<br \/>\nSUM=0<br \/>\ndone<br \/>\necho &#8220;Overall swap used: $OVERALL&#8221;<\/p>\n<p>This will need to be ran as root for it to be able to gather accurate numbers. It will still work even if you don\u2019t, but it will report 0 for any processes not owned by your user. Needless to say, it\u2019s Linux only. The output is ordered alphabetically according to your locale (which admittedly isn\u2019t a great thing since we\u2019re dealing with numbers), but you can easily apply your standard shell magic to the output. For instance, to find the process with most swap used, just run the script like so:<\/p>\n<p>$ .\/getswap.sh | sort -n -k 5<br \/>\nDon\u2019t want to see stuff that\u2019s not using swap at all?<\/p>\n<p>$ .\/getswap.sh | egrep -v &#8220;Swap used: 0&#8221; |sort -n -k 5<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre><code>#!\/bin\/bash\r\n\r\n    # find-out-what-is-using-your-swap.sh\r\n    # -- Get current swap usage for all running processes\r\n    # --\r\n    # -- rev.0.3, 2012-09-03, Jan Smid          - alignment and intendation, sorting\r\n    # -- rev.0.2, 2012-08-09, Mikko Rantalainen - pipe the output to \"sort -nk3\" to get sorted output\r\n    # -- rev.0.1, 2011-05-27, Erik Ljungstrom   - initial version\r\n\r\n\r\nSCRIPT_NAME=`basename $0`;\r\nSORT=\"kb\";                 # {pid|kB|name} as first parameter, [default: kb]\r\n[ \"$1\" != \"\" ] &amp;&amp; { SORT=\"$1\"; }\r\n\r\n[ ! -x `which mktemp` ] &amp;&amp; { echo \"ERROR: mktemp is not available!\"; exit; }\r\nMKTEMP=`which mktemp`;\r\nTMP=`${MKTEMP} -d`;\r\n[ ! -d \"${TMP}\" ] &amp;&amp; { echo \"ERROR: unable to create temp dir!\"; exit; }\r\n\r\n&gt;${TMP}\/${SCRIPT_NAME}.pid;\r\n&gt;${TMP}\/${SCRIPT_NAME}.kb;\r\n&gt;${TMP}\/${SCRIPT_NAME}.name;\r\n\r\nSUM=0;\r\nOVERALL=0;\r\n    echo \"${OVERALL}\" &gt; ${TMP}\/${SCRIPT_NAME}.overal;\r\n\r\nfor DIR in `find \/proc\/ -maxdepth 1 -type d -regex \"^\/proc\/[0-9]+\"`;\r\ndo\r\n    PID=`echo $DIR | cut -d \/ -f 3`\r\n    PROGNAME=`ps -p $PID -o comm --no-headers`\r\n\r\n    for SWAP in `grep Swap $DIR\/smaps 2&gt;\/dev\/null| awk '{ print $2 }'`\r\n    do\r\n        let SUM=$SUM+$SWAP\r\n    done\r\n\r\n    if (( $SUM &gt; 0 ));\r\n    then\r\n        echo -n \".\";\r\n        echo -e \"${PID}\\t${SUM}\\t${PROGNAME}\" &gt;&gt; ${TMP}\/${SCRIPT_NAME}.pid;\r\n        echo -e \"${SUM}\\t${PID}\\t${PROGNAME}\" &gt;&gt; ${TMP}\/${SCRIPT_NAME}.kb;\r\n        echo -e \"${PROGNAME}\\t${SUM}\\t${PID}\" &gt;&gt; ${TMP}\/${SCRIPT_NAME}.name;\r\n    fi\r\n    let OVERALL=$OVERALL+$SUM\r\n    SUM=0\r\ndone\r\necho \"${OVERALL}\" &gt; ${TMP}\/${SCRIPT_NAME}.overal;\r\necho;\r\necho \"Overall swap used: ${OVERALL} kB\";\r\necho \"========================================\";\r\ncase \"${SORT}\" in\r\n    name )\r\n        echo -e \"name\\tkB\\tpid\";\r\n        echo \"========================================\";\r\n        cat ${TMP}\/${SCRIPT_NAME}.name|sort -r;\r\n        ;;\r\n\r\n    kb )\r\n        echo -e \"kB\\tpid\\tname\";\r\n        echo \"========================================\";\r\n        cat ${TMP}\/${SCRIPT_NAME}.kb|sort -rh;\r\n        ;;\r\n\r\n    pid | * )\r\n        echo -e \"pid\\tkB\\tname\";\r\n        echo \"========================================\";\r\n        cat ${TMP}\/${SCRIPT_NAME}.pid|sort -rh;\r\n        ;;\r\nesac\r\nrm -fR \"${TMP}\/\";\r\n\r\n\r\n<\/code><\/pre>\n<pre><code>#!\/bin\/bash\r\ngrep VmSwap \/proc\/[0-9]*\/status | awk -F':' -v sort=$1 '\r\n  {\r\n    split($1,pid,\"\/\") # Split first field on \/\r\n    split($3,swp,\" \") # Split third fireld on space\r\n    cmdlinefile = \"\/proc\/\"pid[3]\"\/cmdline\" # Build the cmdline filepath\r\n    getline pname[pid[3]] &lt; cmdlinefile # Get the command line from pid\r\n    swap[pid[3]] = sprintf(\"%6i %s\",swp[1],swp[2]) # Store the swap used (with unit to avoid rebuilding at print)\r\n    sum+=swp[1] # Sum the swap\r\n  }\r\n  END {\r\n    OFS=\"\\t\" # Change the output separator to tabulation\r\n    print \"Pid\",\"Swap used\",\"Command line\" # Print header\r\n    if(sort) {\r\n      getline max_pid &lt; \"\/proc\/sys\/kernel\/pid_max\"\r\n      for(p=1;p&lt;=max_pid;p++) {\r\n        if(pname[p]) print p,swap[p],pname[p] # print the values\r\n      }\r\n    } else {\r\n      for(p in pname) { # Loop over all pids found\r\n        print p,swap[p],pname[p] # print the values\r\n      }\r\n    }\r\n    print \"Total swap used:\",sum # print the sum\r\n  }'<\/code><\/pre>\n<pre><\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever logged in to a server, ran free, seen that a bit of swap is used and wondered what\u2019s in there? It\u2019s usually not very indicative of anything, or even overly helpful knowing what\u2019s in there, mostly it\u2019s a curiosity thing.<\/p>\n<p>Either way, starting from kernel 2.6.16, we can find out using smaps [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5555"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5555"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5555\/revisions"}],"predecessor-version":[{"id":5556,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/5555\/revisions\/5556"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5555"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5555"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5555"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}