diff --git a/swap/README.md b/swap/README.md new file mode 100644 index 0000000..a8a16e7 --- /dev/null +++ b/swap/README.md @@ -0,0 +1,92 @@ +

Swap

+ +

+ A bash script that will kill the swap file, wait for the swap to flush.
+ Then bring the swap file back up. +

+ +## Folder Structure + +If you have not done so already, create a new folder called `scripts` in your **~home** folder. This is where all your scripts will go. + +Inside your `scripts` folder create a new folder called `swap` + +When done your folders structure should look like this: `/home/[USERNAME]/scripts/swap` + +## Set Up + +The script will need elevated permissions in order to execute with out human intervention. To do so we need to make some changes to the `sudoers` file. + +Here is how you do it: + +1. Open terminal, type: `sudo visudo` to open the file. + +2. Add these commands to second to last section. + + ```bash + # + # Locations to swap files. + # Change [USERNAME] to your own + # + ALL ALL =(ALL) NOPASSWD: /home/[USERNAME]/scripts/swap.sh + ALL ALL =(ALL) NOPASSWD: /home/[USERNAME]/Desktop/swap.sh + ``` + +3. Exit and Save file. + + * `CTRL + X` to exit file + * `Y` to save file + * `ENTER` to write file + +## Script + +### **`swap.sh`** + +Get the source code from this repo and save it as `swap.sh` in the **swap** folder created in the previous step. +Next we need to change the permissions of this file so that the server can read it. + +1. Right click on `swap.sh` then Properties. + +2. Click on `Permissions` tab at top. + +3. Make sure Owner & Group reflect your `username` with `Read and Write` access to all. + +4. Check the `Execute` box to allow executing the file as a program. + +5. Close out the window. + +### **`swap.desktop`** + +Now we need to make the `swap.sh` script act like a regular application. Here is how you do it: + +Get the source code from this repo and save it as `swap.desktop` in the **swap** folder created in the previous step. + +Next we need to change the permissions of this file so that the server can read it. + +1. Right click on `swap.desktop` then Properties. + +2. Click on `Permissions` tab at top. + +3. Make sure Owner & Group reflect your `username` with `Read and Write` access to all. + +4. Check the `Execute` box to allow executing the file as a program. + +5. Close out the window. + +## Link + +We now can create a link to this new application and put it anywhere we want. Here is how you do it: + +1. Single click on the new **swap** application. It should be activated and indicated by changing to a new color. + +2. While **swap** is activated/highlighted, go to `Edit` > `Make Link` + +You should now see a new icon with an arrow on it. That is your new link that you can then place on your desktop. Thats where mine is. + +## Execute + +If you placed your linked file on the desktop all you have to do is **double click** the icon and the script will will call and execute `swap.sh`. + +The script will open a terminal and print out each step. While the script is running, (if you have **System Monitor** open) you will see the Swap line spike for a few seconds. Then it will die out. Once its dead the script waits 30 seconds for everything to flush out the system and then the script will enable **swap**. + +At this point swap should be at zero or super low, basically how it would be after a full system reboot. \ No newline at end of file diff --git a/swap/swap.desktop b/swap/swap.desktop new file mode 100644 index 0000000..9755e72 --- /dev/null +++ b/swap/swap.desktop @@ -0,0 +1,16 @@ +# +# Change [USERNAME] to your own name. +# +# + +[Desktop Entry] +Name=Swap +Comment=clear the swap memory. + +# This version will prompt user to close window +#Exec=gnome-terminal -- bash -c '/home/[USERNAME]/scripts/swap/swap.sh; read -p "Press enter to close..."' +Exec=gnome-terminal -- bash -c '/home/[USERNAME]/scripts/swap/swap.sh' + +Icon=bash +Terminal=false +Type=Application diff --git a/swap/swap.sh b/swap/swap.sh new file mode 100755 index 0000000..61fb015 --- /dev/null +++ b/swap/swap.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +# Turn off swap +echo "Executing swapoff..." +sudo /sbin/swapoff -a + +# Wait until swap is fully off +while [ $(free | grep Swap | awk '{print $3}') -gt 0 ]; do + echo "Waiting for swapoff to complete..." + sleep 5 +done + +# Wait an additional 30 seconds +echo "Swapoff completed. Waiting for 30 seconds..." +sleep 30 + +# Turn swap back on +echo "Executing swapon..." +sudo /sbin/swapon -a