shell script (disk space, service status)

1. Determine whether the current disk remaining space is 20G. If it is less than 20G, an alarm email will be sent to the administrator, and the disk remaining space will be checked once a day.

Step 1: Create a script named shell1.sh as follows:

vim shell1.sh

Step 2: Make a plan In the shell1 file, the command is as follows:

#!/bin/bash
test1=`df -m | grep -w "/" | tr -s " " | cut -d " " -f4`
str1="您最近磁盘小于20G,请及时查看"
if [ "$test1" -lt 20000 ]
then    
        echo "$str1" | mail -s "$str1" [email protected]
fi      

Step 3: Modify the send in the mailbox file and try to send the file

vim /etc/s-nail.rc

pass: When the following situation occurs: Because it is an Alibaba Cloud server, port 25 may be blocked, and port 465 must be used instead, so pay attention to adding the port to the smtp configuration in the configuration content:

set smtp=smtp.exmail.qq.com:465

Step Three: Install the Service

yum install postfix  s-nail

  Step 4: Try to send to the administrator email

 echo "test 1" |s-nail -s "title" [email protected]

 Step 5: Detect and try to run

Add permissions to the file and run the file

chmod +x shell1.sh
./shell1.sh

2. Determine whether the web service is running (1. Check whether the program is running by checking the process, 2. Check whether the program is running by checking the port), if not, start the service and configure firewall rules.

Step 1: Add httpd service

yum install httpd  -y 

Step 2: Create script shell2.sh

vim shell2.sh

Process judgment:


#!/bin/bash

num=$(ps -ef | grep httpd | grep -v grep | wc -l)
if [ $num -ge 1 ]
then
        echo"httpd is running"
else
        systemctl restart httpd
        systemctl stop firewalld
fi

Step Three: Test the Service

 

 Step 4: Modify the port in the script

Port judgment:

Step 5: Test the service

 3. Use the curl command to access the web service of the second question to see if it can be accessed normally. If it can be accessed normally, it will return web server is running; if it cannot be accessed normally, it will return a status code of 12.

Step 1: Write the shell3.sh file as follows:

#!/bin/bash

curl -s 192.168.170.128 > /dev/null
if [[ $? = 0 ]]
then
        echo " web server is runnning "
else
        exit 12
fi
~               

Step Two: Test

Guess you like

Origin blog.csdn.net/m0_68976043/article/details/130550895