Lacking a swapinfo
(*BSD) ,swapon -s
(swap usage) is the next best thing.
Any of the following should do the trick, setting $?
to 0 if one or more swaps are set up:
/sbin/swapon -s | grep -q /dev
grep -q /dev /proc/swaps
free | awk '/Swap/{ exit(!$2)}'
awk '/SwapTotal/ {exit (!$2)}' /proc/meminfo
The last two commands are probably the most robust, the first two won't (as is) confirm the existence of swap when only swap files are used (somewhat unusual, but possibly found during an OS installation, or on diskless systems which swap over NFS).
You can use this instead to detect (or count, use -c
instead of -q
) swaps:
grep -qE "(partition|file)" /proc/swaps
(This will also count swap on /dev/nbd
network block devices, swap block devices are counted as "partition" swap devices, at least up until linux-3.13.5.)