Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- free_memory=$(free -m -l -t -h | awk '/Mem/ {print $4}')
- create_swap_if_needed() {
- if [[ $free_memory == 0 ]]; then
- echo "Creating swap file..."
- sudo sh -c "
- swapoff
- dd if=/dev/zero of=/swapfile bs=1G count=4 status=progress; sync
- mkswap /swapfile
- swapon /swapfile
- "
- fi
- echo "$free_memory"
- }
- check_for_reboot() {
- if [[ $free_memory == 0 ]]; then
- echo "Free memory is 0, rebooting..."
- sudo reboot
- else
- echo "Free memory is sufficient."
- fi
- }
- main() {
- free_memory=$(create_swap_if_needed)
- check_for_reboot
- }
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement