Shell Scripting Exercises

Source: Internet
Author: User
Tags disk usage

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.