3.4 KiB
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:
-
Open terminal, type:
sudo visudo
to open the file. -
Add these commands to end of the file.
# # 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 @includedir /etc/sudoers.d [USERNAME] ALL=(ALL) NOPASSWD: /bin/systemctl restart * [USERNAME] ALL=(ALL) NOPASSWD: /sbin/swapoff [USERNAME] ALL=(ALL) NOPASSWD: /sbin/swapon
-
Exit and Save file.
CTRL + X
to exit fileY
to save fileENTER
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.
-
Right click on
swap.sh
then Properties. -
Click on
Permissions
tab at top. -
Make sure Owner & Group reflect your
username
withRead and Write
access to all. -
Check the
Execute
box to allow executing the file as a program. -
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.
-
Right click on
swap.desktop
then Properties. -
Click on
Permissions
tab at top. -
Make sure Owner & Group reflect your
username
withRead and Write
access to all. -
Check the
Execute
box to allow executing the file as a program. -
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:
-
Single click on the new swap application. It should be activated and indicated by changing to a new color.
-
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.