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

Note:

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:

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

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

- To list the session created by the screen:

You should see

2. Run script on screen

- You create a file (moniter.sh) 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.

- Set execute permissions(only run via root):

- Create a new session with the moniter name:

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

You should see

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

Comments

    • namhuy says

      nice, thanks for sharing. Does sysdig “eat” alot of resource? I will try it on my testing box later ^^

Leave a Reply