Skip to content

Instantly share code, notes, and snippets.

@Ragdata
Last active January 7, 2023 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ragdata/d27121b20acd30cd7057368302c5cdf2 to your computer and use it in GitHub Desktop.
Save Ragdata/d27121b20acd30cd7057368302c5cdf2 to your computer and use it in GitHub Desktop.
Shell script to help manage swap space - cp to /usr/bin and chmod +x
#!/bin/sh
CMD=$1
SIZE=$2
create()
{
if [ -z $SIZE ]; then
swapsize=1024
else
swapsize=$SIZE
fi
sudo /bin/dd if=/dev/zero of=/var/swap bs=1M count=$swapsize
sudo /sbin/mkswap /var/swap.1
sudo /sbin/swapon /var/swap.1
echo "DONE!"
}
remove()
{
status
retval=$?
if [ $retval -eq 0 ]; then
sudo /sbin/swapoff /var/swap.1
fi
}
status()
{
swapon -s | grep -q "/var/swap.1"
swaptest=$?
if [ $CMD = "status" ]; then
if [ $swaptest -eq 0 ]; then
echo "SWAP EXISTS"
else
echo "SWAP NOT FOUND"
fi
else
if [ $swaptest -eq 0 ]; then
return 0
else
return 1
fi
fi
}
case "$1" in
create)
create
;;
remove)
remove
;;
status)
status
;;
*)
N=~/swap
echo "Usage: $N (create|remove|status) [size]";
;;
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment