After writing the article How do I add a swap partition in Linux Centos on VPS VULTR was faced with the fact that some users are having difficulty with creating a swap file. Decided to write a little script, but rather a set of commands to fully automate the process.
Automatically create swap file in CentOS
If you got a server without partition or swap file, to restore justice, namely to create the SWAP size is 512 MB, run the following code in the console with root privileges:
1 2 3 4 5 6 |
dd if=/dev/zero of=/swapfile bs=1M count=512 mkswap /swapfile swapon /swapfile echo -e '\n/swapfile swap swap defaults 0 0' >> /etc/fstab chown root:root /swapfile chmod 0600 /swapfile |
Copy the entire text and paste into the console at once. It is not necessary to enter all commands individually.
Then restart the MySQL server reboot and make sure that everything works.
Description of the used script commands, that is that what does, read the previous article How do I add a swap partition in Linux Centos on VPS VULTR.
Testing and where it works
I tested this code on the virtual server of VULTR. It should also run on other virtual servers with hardware virtualization system type of KVM and VmWare. Of course it works on dedicated servers.
The script will not run on virtual servers for virtualization OpenVZ. But here the case is not in the script, and that you cannot create by definition OpenVZ a virtual memory file.
The script checks for the existence of virtual memory as a swap partition and the swap file. Please check with the commands:
1 |
# free |
or
1 |
# swapon -s |
It is desirable to make this test and after the script and the server restarts, make sure that everything works.
In particular the swapon-s must issue the following
1 2 3 |
# swapon -s Filename Type Size Used Priority /swapfile file 524280 0 -1 |
Leave a Reply