Tracking centos server with bash script

In this article I will show you how to use bash shell to test the system periodically, to check the connection port 80, server load, queue exim, and mysql process … The script will automatically notify you via email if there are anything unusual.

Script will use a while loop to run every 2 minutes and check whether the case is running on the server. To run periodically, you can use command : crontab or screen


In this tutorial I will use screen on CentOS 6 (x64):

1. Install screen

Tools screen will help you to run multiple sessions on a terminal session and keep it alive unless you cancel or restart the server.
This utility is most effective when you want to install a program, and take the time that you are connecting to a remote ssh.

– To install screen you just use yum command:

# yum install screen -y

– To create a new session you use the command :screen -S Name_sesion_or_jobs

# screen -S monitor

– To exit the current screen, and work continues:
Press Ctr + A + D

– To list the session created by the screen:

You should see
[root@Mysql02 ~]# screen -ls
There are screens on:
29823.mysql_moniter     (Detached)
21871.pts-0.Mysql02     (Attached)
2 Sockets in /var/run/screen/S-root.

2. Run script on screen

– You create a file ( with the code below : (don’t forget to set your email address below “### Send mail alert and attachments log file” line near the end of the file.

## Moniter server v1
server=`uname -a|awk {'print $2'}`
ps_view=`ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5`
ps_view2=`ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5`
echo -n "Password: "
read -s password
echo "Server Moniter"
if [ $password != $pass ] ; then
echo "Wrong password"

### File Logs
while true
echo "### Load $num_load ####" >> $log
echo -n "**** load: " >> $log
load1=`w|grep load`

### Tracking number php process
num_php=`ps aux|grep php|grep -v root|wc -l`
w|grep load >> $log
echo "**** Process php" >> $log
echo  "**** Nummber process php: $num_php" >> $log
ps aux|grep php|grep -v root >> $log

### Print top process
echo  "**** Top Process: $ps_view" >> $log

### Print number connect port 80
echo -n "***** Connect Port 80:" >> $log
netstat -an|grep :80|wc -l >> $log

### Print top 5 IP connect port 80
echo "***** Top 5 Ip" >> $log
netstat -an|grep :80 |awk '{print $5}'|cut -d":" -f1|sort|uniq -c|sort -n|tail -n 5 >> $log
i=`w|grep load|sed s/average:/*/|cut -d*  -f2|cut -c1-3| sed -e 's/\.//g'` ## Number the current load of server
load=10 # Default load, if the server load is greater than (10), the script will send mail alerts.
if [ $i -lt $load ]; then
echo "**** Load  $i" >> $log
echo "**** ok" >> $log
echo "*** Load $i" >> $log
echo "**** Not oke" >> $log
echo  "**** Top Process: $ps_view" >> $log
echo  "**** Top Process: $ps_view2" >> $log

Send mail alert and attachments log file
echo "**** $load1 - Load Current: $i"|mail -s "$server" < $log ### Conducting the service startup if you want service httpd restart service mysql restart echo "*** Service restarted" >> $log
echo "-------------------" >> $log
## will check every 2 minute
sleep 120

– Set execute permissions(only run via root):

# chmod 0700

– Create a new session with the moniter name:

# screen -S moniter

– At the command prompt, run the script with password: namhuy

# ./

You should see

[root @ Mysql02 ~] #./

– You can view logs at: log2.txt in your email if your server load is greater than 10



Recommended For You

About the Author: Linux Tips

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.