Note: you must first install an email server such as Postfix or Sendmail.
1. Apache
#!/bin/bash#apache.shnc -w2 localhost 80 if[ $? -ne 0 ]thenecho "apache is down,please check" | mail [email protected] -s "apache is down"/usr/local/apache2/bin/apachectl restartfi
Note:
NC goes to a port.-W2 indicates entering 2 seconds. If the port is enabled for 2 seconds, it automatically exits. If the port is not enabled, it exits directly.
$? Is the result of the previous execution. 0 indicates that the execution is successful. If the port is enabled, non-0 indicates that the execution fails, and the port is disabled.
If email sending is disabled on port 80 and the service is restarted
2. Hard Disk Quota
#!/bin/bash#disk.shnum=`df |awk ‘NR==3{print int($4)}‘`if [ $num -ge 20]thenecho "disk space is ${num}%, now >20%" | mail [email protected] -s "disk space >22%"fi
Note:
Num = 'df | awk' Nr = 3 {print int ($4)} '': Convert the value of column 3rd in the 4th rows of the DF execution result to int and assign the value to num.