Skip to content

Instantly share code, notes, and snippets.

@riyad
Last active January 10, 2022 14:32
Show Gist options
  • Save riyad/3bd345c9786e8ad2602a0e42e3e14f20 to your computer and use it in GitHub Desktop.
Save riyad/3bd345c9786e8ad2602a0e42e3e14f20 to your computer and use it in GitHub Desktop.
List all processes and how much swap they use.
#!/bin/bash
#
# Riyad Preukschas <riyad@informatik.uni-bremen.de>
# License: Mozilla Public License 2.0
# SPDX-License-Identifier: MPL-2.0
#
# List all processes and how much swap they use.
# The output format is: "<swap used> : <pid> : <process name>"
#
# To get the top 10 processes using swap you can run:
# lsswap | sort -hr | head -n10
files=(/proc/*/status)
exec awk '
BEGINFILE {
# skip files that cannot be opened
if ( ERRNO != "" ) {
nextfile
}
}
/^Name:/ { name = $2 }
/^Pid:/ { pid = $2 }
/^VmSwap:/ { vmswap = $2$3 }
ENDFILE {
# skip processes that cannot be swapped and processes not using swap at all
if (length(vmswap) > 0 && vmswap != "0kB") {
print vmswap " : " pid " : " name
}
}
' "${files[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment