Skip to content

Instantly share code, notes, and snippets.

@shinenelson
Last active May 13, 2022 23:43
Show Gist options
  • Save shinenelson/a8a5550eaaefd66658a6d1f10ffbe4dc to your computer and use it in GitHub Desktop.
Save shinenelson/a8a5550eaaefd66658a6d1f10ffbe4dc to your computer and use it in GitHub Desktop.

swap?

parse output of free -m|--mebi and exits with an exit code indicating whether it is safe to put whatever memory is currently in swap memory back into physical memory.

this script is not designed to ( and will not ) run on its own as with any awk script.

usage

basic

free --mebi | awk -f /path/to/swap-status.awk

free swap

free --mebi | awk -f /path/to/swap-status.awk && [sudo] swapoff --all && [sudo] swapon --all

DEBUG log level

set some basic logging to debug the script by setting the DEBUG variable for awk.

free --mebi | awk -v DEBUG=true -f /path/to/swap-status.awk

shell alias

set a shell alias that you can call to check if it is safe to empty the swap back into physical memory

alias swap?="free --mebi | awk -v DEBUG=$DEBUG -f $HOME/dev/awk/swap-status/swap-status.awk"
shell alias usage
swap?			# non-debug usage
swap? DEBUG=true	# set DEBUG=true for one single usage
more elaborate, boring way to set the DEBUG flag
DEBUG=true
swap?
DEBUG=''

single-line to do the above ( I cannot think of why you would want to use this rather than the one in shell alias usage, but here it is anyway ) DEBUG=true && swap? && DEBUG=''

⚠️ disclaimer warning ⚠️

if you forget to unset the DEBUG variable, it might get picked up by other utilities that use it. The variable might not do much with this tiny script, but it could do much more with other bigger utilities. You might get thrown it into a whole spam of debug logs and you might not even remember that you had this flag on.

piping

because this is an awk script, one can naturally assume that it plays nice with pipes. well, it does... mostly. However, not much time has been spent making it pipe-safe.

for example, one obvious way to break the script is to pipe the output of any other utility other than free. or even the output of the human-readable form of free ( free -h ) because it appends the memory unit to the output and you cannot perform binary operations on strings. you should not pipe the human-readable output of any utility to another utility because they are obviously not human.

similarly, the script might not give a desired result if anything other than the outputs of --mebi / --mega because the check for 256MiB might fail or worse make a favorable exit when it is not actually safe ( --gebi / --giga ).

the script, by default, has no kind of parseable output. the script was not designed for the output to be parsed, but the exit code to be used by a logical operator like && or ||. so no, you should not pipe the output of this script to any other utility.

exit code

the script exits normally ( exit code 0 ) if there will be at least 256MiB free after putting all of the memory from swap back into the physical memory or if swap is already empty.

if there will not be enough memory to put all of the swap memory back into physical memory, the script will mostly exit with exit code 255.

there is a fancy exit code logic if the difference is under 256MiB, then the script will exit an exit code with the difference of memory available. It was just dumb color that I decided to add to a boring script.

#!/usr/bin/env awk
# not really necessary, but here for completness
# BEGIN {
# mem = 0;
# swap = 0;
# }
NR == 2 { mem = $4; }
NR == 3 { swap = $3; }
END {
# bail out if swap is unused
if ( swap == 0 ) {
if ( DEBUG == "true" ) { print "swap is empty" }
exit 0
}
msdiff = mem - swap;
if ( DEBUG == "true" ) { print mem, swap, msdiff; }
if ( msdiff > 256 ) {
if ( DEBUG == "true" ) { print msdiff, "available; good to swap"; }
}
else {
if ( DEBUG == "true" ) { print msdiff, "is not > 256; can't swap"; }
# un-necessary fancy exit logic FFS
if ( msdiff < 256 && msdiff > -256 ) {
exit msdiff;
}
else {
exit 255;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment