Shell's enterprise practical work case

Source: Internet
Author: User
Tags web services rsync pkill

Case one:

Monitoring Web Site Directory ( /var/html/www ) If all files are tampered with (the contents of the file have been changed), if there is a file name (e-mail) to print the changes, the scheduled task every 3 minutes to execute once

Monitoring script content:

[[email protected] scripts]# cat web-file.sh#!/bin/bash################################### ############################ file name: web-file.sh# version: v1.0# author:  da ya# organization: [email protected]# created time : 2018#  description:############################################################# #Email = ' [email protected] ' Find_file= ' find /tmp/ -type f ' dir= '/opt/web_file.log ' old_file= (' cat  $Dir |sed -r  ' s#[a-z,0-9]+   (. *) #\1#g ' |paste -s ') new_file= (' $Find _file|paste -s ') function check_ MD5 () {  if [ ! -f  "$Dir"  ];then     md5sum $ find_file >> $Dir   else     md5sum -c  "$Dir"  >/ Opt/check.log 2>&1     if [ $? -ne 0 ];then       mail -s  ' html modify '   $Email  </opt/check.log      fi  fi}function check_file () {  if [  "${#Old_file [*]}"  !=  "$ {#New_file [*]} "  ];then     echo " $New _file "|mail -s " Html  modify "  $Email   fi}function main () {  check_md5  check_file}main


Case TWO:

Writing service management scripts for rsync

Script content:

#!/bin/bash############################################################### file name:  web-file.sh# version: v1.0# author: da ya# organization: [email  protected]# created time : 2018# description:######################################## ######################. /etc/init.d/functions if [ ! -f /etc/rsyncd.conf ]; then  action  ' rsyncd.conf not exists '  /bin/false  exitfistart_cmd= ' Rsync --daemon ' stop_cmd= ' pkill rsync ' port_cmd= ' ss -tunlp|grep 873|wc -l ' START ()   {    if [  "$port _cmd"  -eq 2 ];then       action  ' rsync service is already exists '  /bin/true       exit    else       $start _cmd       action  'Rsync started '  /bin/true    fi}stop ()  {     $stop _cmd     action  ' rsync is stoped '  /bin/true}restart ()  {      $stop _cmd    action  ' rsync is stoped '  /bin/true     sleep 1     $start _cmd    action  ' rsync  is started '  /bin/true}case $1 instart)     START     ;; Stop)     STOP    ;; Restart)     RESTART    ;; Esac


Case THREE:

Monitoring memcache If the service is normal, impersonate the user ( Web client) detection.

Use NC commands plus set/get to simulate detection, and monitor response times and hit ratios.

Script content:

#!/bin/bash############################################################### file name:  memcache.sh# version: v1.0# author: da ya# organization: [email  protected]# created time : 2018# description:######################################## ######################. /etc/init.d/functionsdate_cmd= ' date +%f-%s ' port_cmd= ' nmap 172.16.1.21  -p 11211|awk  ' nr==6{print $2} ' set_cmd= ' printf  ' set key009 0 10  10\r\njiang12345\r\n "|nc 172.16.1.21 11211 ' get_cmd= ' printf " get key009 \r\n "|nc  172.16.1.21 11211|tr -d  ' \ R ' |awk  ' nr==2{print $1} ' restart_cmd= ' systemctl  Restart memcached.service ' if [  "$port _cmd"  !=  "CLOESD"  ];then       if [  "$get _cmd"  !=  "jiang12345"  ];then           action  ' Memcache get key is successfull '  /bin/true       fi   else       $restart _CMDFI


Case FOUR:

Write Nginx service Management scripts and monitor Web services for normal

Manage Service Scripts:

#!/bin/bash############################################################### file name: nginx.sh#  Version: V1.0# Author: da ya# Organization: [email protected]#  created time : 2018# description:######################################################### #####. /etc/init.d/functionsstart_cmd= '/application/nginx/sbin/nginx ' stop_cmd= ' Pkill nginx ' Restart_ Cmd= '/application/nginx/sbin/nginx -s reload ' if [ $# -ne 1 ];then     echo  "Please input $0 start|stop|restart"      Exitficase $1 in 1|start| START)     $start _cmd   action  ' nginx started '  /bin/true;; stop| STOP)     $stop _cmd   action  ' nginx stoped '  /bin/true;; Restart| RESTART)     $restart _cmd   action  ' nginx restarted '  /bin/true;; Esac


Monitoring Web Service scripts:

#!/bin/bash############################################################### file name:  monitor.sh# version: v1.0# author: da ya# organization: [email  protected]# created time : 2018# description:######################################## ##################### #start_cmd = '/application/nginx/sbin/nginx ' stop_cmd= ' Pkill nginx ' port_cmd= ' ss - Tunlp|grep 80|wc -l ' curl_cmd= ' curl -s -i 10.0.0.31 -w  "%{http_code}\n"  -o /dev/null ' if [  "$port _cmd"  -ne 1 ];then     $stop _ cmd    sleep 2     $start _cmd       if [  "$curl _cmd"  -ne 200 ];then        echo   "$curl _cmd" |mail -s  "web service failed"  [email protected]       fifi


Case FOUR:

Implement the MySQL database for sub-library backup

Script content:

#!/bin/bash############################################################### file name:  monitor.sh# version: v1.0# author: da ya# organization: [email  protected]# created time : 2018# description:######################################## ##################### #start_cmd = '/application/nginx/sbin/nginx ' stop_cmd= ' Pkill nginx ' port_cmd= ' ss - Tunlp|grep 80|wc -l ' curl_cmd= ' curl -s -i 10.0.0.31 -w  "%{http_code}\n"  -o /dev/null ' if [  "$port _cmd"  -ne 1 ];then     $stop _ cmd    sleep 2     $start _cmd       if [  "$curl _cmd"  -ne 200 ];then        echo   "$curl _cmd" |mail -s  "web service failed"  [email protected]       fifi


Case FIVE:

Use the For loop to print words with less than 6 alphabetic characters in the following string

I am Oldboy Teacher Welcome to Oldboy training class.

Script content:

#!/bin/bash for I on I am oldboy teacher Welcome to Oldboy Training class do if ["${#i}"-GT 6];then echo $i: ${ #i} else continue Fidone


Case SIX:

Batch create multiple accounts and generate random passwords, create a successful request account name and corresponding password to record to the file

Script content:

Case Seven:

[LVS Master node] manual development Ipvsadm Management LVS script Ip_vs

Implementation:/etc/init.d/lvs {Start|stop|restart}

Script content:

#!/bin/bash############################################################### file name: lvs.sh#  Version: V1.0# Author: da ya# Organization: [email protected]#  created time : 2018-03-24 00:47:17# description:##################################### #########################. /etc/init.d/functionsip_cmd= ' ip addr show eth0|grep  10.0.0.13|wc -l ' START ()  {   if [  "$ip _cmd"  -eq 0 ];then       ip addr add 10.0.0.13/24 dev eth0       ipvsadm -c      ipvsadm --set 30 5  60      ipvsadm -a -t 10.0.0.13:80 -s wrr -p  20      ipvsadm -a -t 10.0.0.13:80 -r  10.0.0.17:80 -g -w 1      ipvsadm -a -t 10.0.0.13:80 -r 10.0.0.18:80 -g  -w 1      action  ' lvs is started '  /bin/true    else      action  ' lvs is already exists '  / Bin/true   fi}stop ()  {      ipvsadm -C       ip addr del 10.0.0.13/24 dev eth0       action  ' lvs is stoped '  /bin/true}restart ()  {       ipvsadm -C      ip addr del 10.0.0.13/24  dev eth0      action  ' lvs is stoped '  /bin/true       ip addr add 10.0.0.13/24 dev eth0       ipvsadm --set&nbSp;30 5 60      ipvsadm -a -t 10.0.0.13:80 -s  wrr -p 20      ipvsadm -a -t 10.0.0.13:80 -r  10.0.0.17:80 -g -w 1      ipvsadm -a -t  10.0.0.13:80 -r 10.0.0.18:80 -g -w 1      action  ' Lvs is restarted '  /bintrue} case $1 in start)    START    ;;  stop)    STOP   ;;  restart)    RESTART   ;;  *)    echo  ' usage: input { start|stop|restart } '     ;; Esac

Shell's enterprise actual work case

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.