20,150,722 Week Three
1. Generate a log file daily according to the format xxxx-xx-xx, for example, the file generated today is 2013-09-23.log, and the disk usage is written to the file.
#vi/usr/local/sbin/crontest1.sh
#!bin/bash
d= ' date +%f ' inverted single quote, referencing the execution result of the system command
logfile= $d. Log $ reference variable value
df-h >/var/log/$logfile View disk usage df-h; view memory usage free
#crontab-E use Crontab-e/-l/-r (delete)/-u (specify user, not add current user)
0 0 * * */bin/sh/usr/local/sbin/crontest1.sh absolute path/bin/sh
-------------------------------------------------------------------------------------------
20,150,723 Thu
2. Running on the server is httpd, write a monitoring script, every minute to check whether the httpd process exists, if found not to start it.
Idea: PS aux to see if the httpd process is started, if it does not start, record the time, and start the httpd service. After starting, again determine if PS aux has a httpd process, if not log error logs, and mail notify the Administrator, Manual Check the cause, and exit the dead loop. Log is not logged if the first time the httpd process exists or if it starts successfully.
#! /bin/bash
##
##
Log=/var/log/httpdcheck.log
A= ' PS aux |grep httpd '
While:; Do
If [-Z $a]; Then
echo "[' Date + '%F%T '] httpd service is off" >> $log
/usr/local/apache2/bin/apachectl Start & >> $log
If [-Z $a]; Then
Mail-s ' htttpd service start fialed. Please check. ' [Email protected] < $log
Exit
Fi
Fi
Sleep 60
Done
Shell Scripting Exercises