Skip to content

Instantly share code, notes, and snippets.

@chuyeow
Created July 2, 2012 09:02
Show Gist options
  • Save chuyeow/3032142 to your computer and use it in GitHub Desktop.
Save chuyeow/3032142 to your computer and use it in GitHub Desktop.
Script to add a swapfile
#!/bin/bash
# Creates a swapfile at SWAPFILE.
SWAPFILE=/mnt/swapfile
SWAPSIZE_MB=7168
SWAP_ALREADY_ON=`swapon -s | grep $SWAPFILE`
if [ -e $SWAPFILE ]; then
if [[ -n $SWAP_ALREADY_ON ]]; then
echo "$SWAPFILE already exists and activated, skipping."
else
echo "$SWAPFILE already exists, activating..."
swapon $SWAPFILE
echo "Swap status:"
swapon -s
fi
else
echo "Creating $SWAPSIZE_MB MB swapfile at $SWAPFILE..."
dd if=/dev/zero of=$SWAPFILE bs=1M count=$SWAPSIZE_MB
mkswap $SWAPFILE
swapon $SWAPFILE
echo "Swap status:"
swapon -s
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment