Skip to content

Instantly share code, notes, and snippets.

@freyes
Created December 18, 2014 14:51
Show Gist options
  • Save freyes/219cf2161dbfec01dae1 to your computer and use it in GitHub Desktop.
Save freyes/219cf2161dbfec01dae1 to your computer and use it in GitHub Desktop.
This Bash script will display how much swap each process is using, in kB
#! /bin/bash
#
# swap.sh: Shows the swap usage of each process
# Author: Robert Love
# source: http://www.quora.com/How-can-you-determine-what-processes-are-using-swap-space
swap_total=0
for i in /proc/[0-9]*; do
pid=$(echo $i | sed -e 's/\/proc\///g')
swap_pid=$(cat /proc/$pid/smaps |
awk 'BEGIN{total=0}/^Swap:/{total+=$2}END{print total}')
if [ "$swap_pid" -gt 0 ]; then
name=$(cat /proc/$pid/status | grep ^Name: |
awk '{print $2}')
echo "${name} (${pid}) ${swap_pid} kB"
let swap_total+=$swap_pid
fi
done
echo
echo "Total: ${swap_total} kB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment