Thứ Bảy, 16 tháng 7, 2016

5 commands to check memory usage on Linux

Memory Usage

On linux, there are commands for almost everything, because the gui might not be always available. When working on servers only shell access is available and everything has to be done from these commands. So today we shall be checking the commands that can be used to check memory usage on a linux system. Memory include RAM and swap.
It is often important to check memory usage and memory used per process on servers so that resources do not fall short and users are able to access the server. For example a website. If you are running a webserver, then the server must have enough memory to serve the visitors to the site. If not, the site would become very slow or even go down when there is a traffic spike, simply because memory would fall short. Its just like what happens on your desktop PC.

1. free command

The free command is the most simple and easy to use command to check memory usage on linux. Here is a quick example
$ free -m
             total       used       free     shared    buffers     cached
Mem:          7976       6459       1517          0        865       2248
-/+ buffers/cache:       3344       4631
Swap:         1951          0       1951

Thứ Sáu, 8 tháng 7, 2016

Tự động restart mysql khi quá tải

Khi quá tải, server sẽ bị đơ, giống như việc máy tính của bạn bị đơ bình thường thôi, để khắc phục hiện tượng này, bạn có thể tạo một file check kiểm tra xem server có quá tải ko, nếu có thì khởi động lại apache. cách làm như sau:
 Bước 1: Tạo file check

Code:
cd /usr/local/
mkdir auto
cd auto
nano -w restart.sh
Dán script sau:
Code:
#!/bin/sh
check=`cat /proc/loadavg | sed 's/\./ /' | awk '{print $1}'`
if [ $check -gt 20 ] //điền 5 cho VPS 512MB Ram, 10 cho 1GB Ram, 20 cho 2GB Ram
then
/etc/init.d/mysqld restart
fi
Lưu lại file, và chmod script về 755
Code:
chmod 755 restart.sh

Bước 2: Tạo file cron
Code:
cd ~
cd /etc/cron.d
nano -w auto_restart
Dán script sau:
Code:
*/5 * * * * root /usr/local/auto/restart.sh >/dev/null 2>&1
Lưu lại file cron để hoàn tất.