Chat / support@radicenter.eu

2 Shell scripti VPS Linuxi kiiremaks kasutamiseks (mem/swap)

Antud lehel on toodud paar skripti serveri kiiremaks kasutamiseks (Testitud Ubuntu 14.04 LTS peal).
NB! Antud skriptide kasutamine on vabatahtlik – Skripti autor ega Radicenter ei ole mingil määral vastutavad skripti ajakohasuse, võimaliku väärkasutuse ja/või toimimise/toimingute eest.

1. Check_Mem – Kontrolli serveri mälu ja kettaruumi
2. add_swap_vps – Skript VPS serverile SWAP faili lisamiseks

1. Check_Mem – Kontrolli serveri mälu ja kettaruumi

Skript kiireks serveri mälu ja kettakasutuse kontrolliks.
(NB! Kasutatav tõrgeteta root kasutaja alt)

1. Loome skriptifaili:

vi .check_mem

(faili muutmiseks vajutage klahvile “i”)
2. Lisage/kopeerige faili järgmine tekst:

#!/bin/bash
echo '-----------------------------------'
echo 'Checking SWAP space:              #'
echo '-----------------------------------'
swapon -s
sleep 5
echo '-----------------------------------'
echo 'Checking free Memory:             #'
echo '-----------------------------------'
free -m
sleep 5
echo '-----------------------------------'
echo 'Checking free space on harddrive: #'
echo '-----------------------------------'
df -h
echo '-----------------------------------'

Salvestamiseks vajutage “Esc” nupule ning kirjutage ssh aknasse:

:wq

 

3. Muutke antud failil failiõigused root kasutajaga käivitamiseks:

chmod 755 .check_mem

Nüüd saate iga kord üle SSH kontrollida kiirelt VPS mälu ja kettakasutust käsuga:

./.check_mem

2. add_swap_vps – Skript VPS serverile SWAP faili lisamiseks

Skript lisab serverile 4GB swap faili kõvaketta ruumi arvelt virtuaalmälu paremaks kasutuseks. Vajalik serveritel (reeglina Starter pakettidel), kus on suuremast koormusest tulenevalt pidevad mäluprobleemid (rakendused crashivad) ja klient ei soovi serveri paketti tõsta.
(NB! Kasutatav tõrgeteta root kasutaja alt)
NB! Antud meetod on soovitada kasutada ainult juhul, kui VPS paketi skaleerimine suuremaks ei ole koheselt võimalik. Antud meetod võib muuta serveri kettakasutust aeglasemaks, kuna kirjutab nö lisa virtuaalvahemälu faili kettale.
Lisainfot, kas swap oleks vajalik ja arutelu selle üle on leitav lehelt: https://wiki.gentoo.org/wiki/Knowledge_Base:Is_swap_space_really_necessary

1. Loome skriptifaili:

vi .add_swap_vps

(faili muutmiseks vajutage klahvile “i”)
2. Lisage/kopeerige faili järgmine tekst:

#!/bin/bash
##################################################
# Script: add_swap_vps                           #
# OS: Ubuntu LTS                                 #
# Author: Kristjan Tarjus                        #
# since: 13.05.2015                              #
# DISCLAIMER:                                    #
# This script is provided as is without any      #
# warranty or confirmation about it's work.      #
# Script author is not and can not be held       #
# responsible for this scipts work or any        #
# misbehaviour or configuration that may happen! #
# ---------------------------------------------  #
# Script is tested on: Ubuntu 14.04 LTS 64bit    #
##################################################
echo '-------------------------------------------'
echo '#  Program to allocate and add swap file  #'
echo '-------------------------------------------'
echo '# By using this scipt end user knows      #'
echo '# how to use it and is solely responsible #'
echo '# for script modifications and of its work#'
echo '-------------------------------------------'
echo ''
echo '-------------------------------------------'
echo '#  Adding new 4GB file for future swap:   #'
echo '-------------------------------------------'
fallocate -l 4G /swap_4GB
echo '-------------------------------------------'
echo '#  Checking if file added:                #'
echo '-------------------------------------------'
ls -lh /swap_4GB
echo '-------------------------------------------'
read -p "If everything was fine - Continue (y/n)?" choice
case "$choice" in
  y|Y ) echo "Yes - adding swap file is a GO";;
  n|N ) echo -e "No - User choosed to abort this script \r\nNB! Please check if file was added and remove it with command: \r\n rm -rf swap_4GB \r\n------------------------"; exit;;
  * ) echo "invalid choice - aborting script"; exit;;
esac
echo '-------------------------------------------'
echo '#  Changing swap file permissions to 600  #'
echo '-------------------------------------------'
chmod 600 /swap_4GB
echo '-------------------------------------------'
echo '# Checking swap file for permissions -rw- #'
echo '-------------------------------------------'
ls -lh /swap_4GB
echo '-------------------------------------------'
read -p "If everything was fine - Continue (y/n)?" choice
case "$choice" in
  y|Y ) echo "Yes - Continuing script work";;
  n|N ) echo -e "No - User choosed to abort this script \r\nNB! Please check if file was added and remove it with command: \r\n rm -rf swap_4GB \r\n------------------------"; exit;;
  * ) echo "invalid choice - aborting script"; exit;;
esac
echo '-------------------------------------------'
echo '#  Setting up swap file:                  #'
echo '-------------------------------------------'
mkswap /swap_4GB
echo '-------------------------------------------'
echo '#  Enabling swap file for use:            #'
echo '-------------------------------------------'
swapon /swap_4GB
echo '-------------------------------------------'
echo '#  Checking if SWAP space is enabled now: #'
echo '-------------------------------------------'
swapon -s
echo '-------------------------------------------'
free -m
read -p "If everything was fine - Do you want to add swap file permanently to system settings (y/n)? (WARNING!) THIS WILL MODIFY SYSTEM FILES!!!" choice
case "$choice" in
  y|Y ) echo "Yes - Continuing script work";;
  n|N ) echo -e "No - User choosed to abort this script \r\n------------------------"; exit;;
  * ) echo "invalid choice - aborting script"; exit;;
esac
echo '-------------------------------------------'
echo '#  Adding SWAP to /etc/fstab file:        #'
echo '-------------------------------------------'
echo '/swap_4GB   none    swap    sw    0   0' >> /etc/fstab
echo '-------------------------------------------'
echo '#  Checking swappiness value (def. 60):   #'
echo '-------------------------------------------'
cat /proc/sys/vm/swappiness
echo '-------------------------------------------'
echo '#  Changing swapiness to 10 for VPS:      #'
echo '-------------------------------------------'
sysctl vm.swappiness=10
echo '-------------------------------------------'
echo '#  Adding swappiness to /etc/sysctl.conf  #'
echo '-------------------------------------------'
echo 'vm.swappiness=10' >> /etc/sysctl.conf
echo '-------------------------------------------'
echo '#  Checking cache pressure (def. 100):    #'
echo '-------------------------------------------'
cat /proc/sys/vm/vfs_cache_pressure
echo '-------------------------------------------'
echo '#  Changing pressure to 50 for VPS:       #'
echo '-------------------------------------------'
sysctl vm.vfs_cache_pressure=50
echo '-------------------------------------------'
echo '#  Adding cache pressure /etc/sysctl.conf #'
echo '-------------------------------------------'
echo 'vm.vfs_cache_pressure = 50' >> /etc/sysctl.conf
echo '-------------------------------------------'
echo -e 'Success - You have configured new 4GB swap file succesfully to your VPS'

Salvestamiseks vajutage “Esc” nupule ning kirjutage ssh aknasse:

:wq

 

3. Muutke antud failil failiõigused root kasutajaga käivitamiseks:

chmod 755 .add_swap_vps

Nüüd saate iga kord üle SSH kontrollida kiirelt VPS mälu ja kettakasutust käsuga:

./.add_swap_vps

Comments are currently closed.

See veebileht kasutab küpsiseid, mis aitavad teha lehe külastamise mugavamaks. Meie veebilehte külastades nõustute meiepoolse küpsiste kasutamisega.
Sain aru.