Shell Exercises (2)

Source: Internet
Author: User

(1)Monitoring 80 Ports
Write a script to determine whether the 80 port of the machine is open, if you do not do anything, if you find that the port does not exist, then restart the httpd service, the concurrent mail notify yourself. Once the script is written, it can be executed every minute, and a dead loop script can be written, and 30s is detected once.

#! /bin/bash[email protected]if netstat -lnp |grep  ':  |grep -q   ' LISTEN ';  then    exitelse    /usr/local/apache2/bin/ apachectl restart >/dev/null 2> /dev/null    python mail.py   $mail   "check_80"   "The 80 port is down."     n= ' Ps aux |grep httpd|grep -cv grep '      if [  $n  -eq 0 ]; then        /usr/local/ apache2/bin/apachectl start 2>/tmp/apache_start.err    fi     if [ -s /tmp/apache_start.err ]; then         python mail.py   $mail    ' apache_start_error '     ' cat /tmp/ Apache_start.err '     fifi

(2) domain name Agent

There is a machine in the intranet can not be connected to the network, so there is no way to use Yum, consider using iptables NAT forwarding Internet, but for some reasons, abandoned use. So think of Nginx proxy, the principle is very simple. A cannot access 1 sites, B can access, A and B can communicate in the intranet, so B can be used as a proxy. And can restrict access to the source IP, the configuration file is as follows:

server {    listen       80;     server_name aaa.com bbb.com  ccc.com ddd.com eee.com;     location / {        resolver 119.29.29.29;         proxy_pass       http://$host;         proxy_set_header Host       $host;        proxy_set_header x-real-ip  $remote _addr;         allow 192.168.5.0/24;         deny all;    } Description: Here the 119.29.29.29  is a DNS IP, specified with resolver. If the B machine Intranet IP is  192.168.5.11, only need to add a hosts192.168.5.11 aaa.com bbb.com ccc.com ddd.com eee.com on a

(3) backing up the database

Design a shell script to back up the database, first save a copy of the data on the local server, and then remotely copy a copy, save the week's data locally, save for one months remotely.
Assume that we know the MySQL root account password, to back up the library is discuz, the local backup directory is/bak/mysql, the remote server IP is 192.168.123.30, Remote provides a rsync service, the backup address is 192.168.123.30::backup. After the script is written, it needs to be added to Cron and executed 3 o'clock in the morning every day.

#! /bin/bash### backup mysql data### writen  by aming.path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bind1= ' Date +%w ' d2= ' date +%d ' pass= "Your_mysql_password" bakdir=/bak/mysqlr_bakdir=192.168.123.30::backupexec  1>/var/log/mysqlbak.log 2>/var/log/mysqlbak.logecho  "mysql backup begin  at  ' date + "%f %t" "." Mysqldump -uroot -p$pass --default-character-set=gbk discuz > $bakdir/$d 1.sqlrsync  -az  $bakdir/$d 1.sql  $r _bakdir/$d 2.sqlecho  "mysql backup end at  ' date  + "%f %t" "." Then add cron0 3 * * * /bin/bash /usr/local/sbin/mysqlbak.sh 

(4) Automatic restart of nginx service

The server runs the LNMP environment, there are always 502 phenomena in the near future. 502 Status code for website access, 200 normal, 502 error is the most common error status code nginx. Since 502 is only temporary, and as long as a restart of the PHP-FPM service 502 disappears, but does not restart, it will continue for a long time. So it is necessary to write a monitoring script, monitoring the Access log status code, once the occurrence of 502, then automatically restart the PHP-FPM.

We set:

    1. Access_log/data/log/access.log

    2. Script dead Loop, detected every 10s (assuming that the number of log bars per 10s clock is about 300)

    3. The way to restart PHP-FPM is/etc/init.d/php-fpm restart

#! /bin/bashlog=/data/log/access.logn=10while :; do     # #因为10秒钟大概产生300条日志     tail -n 300  $log  >  /tmp/log    n_502= ' grep -c  '  502 "'  /tmp/log '      if [  $n _502 -ge  $N  ]; then         # #记录系统的状态         top -bn1 >/tmp/' Date +%H%M%S '- top.log        vmstat 1 5 >/tmp/' Date +%H%M%S '- Vm.log        /etc/init.d/php-fpm restart 2>/dev/null         # #重启php-fpm Service, should be suspended for 1 minutes, and then continue to detect every 10s          sleep 60    fi         sleep 10done 

(10) Delete the letters in the text

Requirement: Delete the line containing the letters in the first 5 lines of a text document and delete all the letters from 6 to 10 lines.

Assuming that the text name is 1.txt and the number of lines of text is greater than 10, the script is as follows #!/bin/bash# #先获取该文本的行数nu = ' wc-l 1.txt |awk ' {print '} ' # #对前5行进程处理for i in ' SEQ 1 5 ' Do # # Use SED to assign the contents of each row to the variable l= ' sed-n ' $i "P 1.txt ' # #用grep determine if the match letter,-v inversion,-Q does not output the content if echo $l |grep-vq ' [a-za-z] ' Thenech o $l fidone# #对6-10 lines do delete letter processing for i in ' seq 6 ' do l= ' Sed-n "$i" P 1.txt ' echo $l |sed ' s/[a-za-z]//g ' done# #剩余的直接输出fo R i in ' seq one $nu ' do sed-n "$i" P 1.txtdone# #若想把更改内容写入到1. txt, you need to redirect the above to a text, then delete 1.txt, and rename the file you just redirected to 1.txt


This article is from "a Man & A computer" blog, please be sure to keep this source http://juispan.blog.51cto.com/943137/1951343

Shell Exercises (2)

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.