> Adding **swap** to the repo
This commit is contained in:
Eugene Amos 2023-12-12 23:39:20 -08:00
parent fff595685f
commit 715940f9e8
3 changed files with 127 additions and 0 deletions

92
swap/README.md Normal file
View File

@ -0,0 +1,92 @@
<h1 align="center">Swap</h1>
<p align="center">
A bash script that will kill the <strong>swap file</strong>, wait for the swap to flush.<br/>
Then bring the <strong>swap file</strong> back up.
</p>
## 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.

16
swap/swap.desktop Normal file
View File

@ -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

19
swap/swap.sh Executable file
View File

@ -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