Working case of shell script (updated at 05-30 ........)

Source: Internet
Author: User
In the case of shell script in work (update...) -- Linux general technology-Linux technology and application information, the following is a detailed description. [I = s] This post was last edited by naihua at, May 30 ,.

This article mainly sorts out the scripts used in my work and serves as an example. It aims to help some friends who have requirements and some who are not well-written. I hope you can give me some advice, in order to do better:
1. Restart the Service Program
Cat restart_jboss.sh
#! /Bin/bash
Source ~ /. Bash_profile
Check_pid = 'ps-ef | grep jboss | grep org. jboss. logmanager | grep jboss-7.0.2GA | awk '{print $2 }''
Echo "check_pid: $ check_pid"
Kill $ check_pid
Sleep 4
If [! -Z $ check_pid]
Then
Kill-9 $ check_pid
Sleep 2
Fi
Cd/usr/local/jboss-7.0.2GA/bin
Nohup./standalone. sh> nohup. out 2> & 1 &
Here, jboss is the service name. For other service restart scripts, refer;
2. Log Cutting
For some access logs, You need to record them separately every day. You can use the script to define them:
Cat cut_nginx_logs.sh
#! /Bin/bash
#### Cut nginx's log
Mv/usr/local/nginx/logs/access. log/usr/local/nginx/logs/access. 'date "+ % m-% d" '. log
Kill-HUP 'cat/usr/local/nginx/logs/nginx. pid'
Define the log name in the format of "access. Monthly-Daily". For more information about script cutting, see
3. rsync resource synchronization script:
This is because when synchronizing resources, instead of copying the entire directory together, it is some sub-directories under the parent directory. In this way, it will be troublesome to synchronize sub-directories one by one:
Cat rsync_xbtx.sh
#! /Bin/bash
Rsync-avz \
-- Include = *. jar \
-- Include = conf \
-- Include = conf/xbtx /\
-- Include = conf/xbtx/xls \
-- Exclude = conf/xbtx /*\
-- Exclude = conf /*\
-- Include = Res \
-- Include = Res /*\
-- Exclude = /*\
Root @ server ip:/usr/local/applications/zyx/xbtx // usr/local/xbtx/
This script is used to locally synchronize resources under serverip/usr/local/applications/zyx/xbtx /.
4. the jboss program is sometimes unable to access the database due to network access and program exceptions, and you cannot monitor it all the time and learn the error in time, check the log to see if there is any error. If yes, call the restart script of the jboss program.
Cat read. sh
#! /Bin/bash
# This's script to check jboss error, about oracle connect;
Cd/home/jboss/jboss-4.2.2.GA/bin
Tail nohup. out-c 500 k | grep "java. SQL. SQLException: An attempt by a client to checkout a Connection has timed out"> read.txt
If grep-q "java. SQL. SQLException" read.txt; then
Cd/home/dustin
./Restart_jboss.sh
Else
Echo "not found this error"
Fi
Exit 0
You can put the script in the crond task plan and check for updates every 10 minutes.
5. Backup: Backup for important services is indispensable. When a misoperation is performed or the server is abnormal, restoring the backup file will bring you great convenience.
#! /Bin/bash
Source ~ /. Bash_profile
# Name num according to the server ip Address
Num = 46
Date = 'date + "% Y _ % m _ % d "'
# Directory required for storing backup files
Cd/home/frank
If [! -D server $ Num]
Then
Mkdir server $ Num;
Fi

ServerBackup =/home/frank/server $ Num
If [! -D scripts]
Then
Mkdir-p $ ServerBackup/jboss-7.0GA
Fi

# Backup jboss-7.0GA, using the rsync command
Rsync-av/usr/local/jboss-7.0.2GA/standalone/deployments/*. war $ ServerBackup/jboss-7.0GA/

Rsync-av/usr/local/jboss-7.0.2GA/standalone/configuration $ ServerBackup/jboss-7.0GA/

Rsync-av/usr/local/jboss-7.0.2GA/standalone/lib $ ServerBackup/jboss-7.0GA/

Rsync-av/usr/local/jboss-7.0.2GA/bin/conf $ ServerBackup/jboss-7.0GA/
# Changing the owner and group
Chown-R frank: root/home/frank
# Packaging
Tar-cvf server‑num..tar.gz server $ Num
Echo
6. Sometimes you need to find the service name with the most http access from the log
Ideas:
Find the most accessed service name. Check the nginx output log first.
221.179.8.24--[30/May/2012: 15: 33: 55 + 0800] "GET/comum/request? Pver = 1 & ac = checkphone & username = fkk148 & data = 13112 & imsi = 460027092382555 & plat = 14 & kver = 46 & SC = 8 & prj = 7635 & smsc = 13800290500 & mnc = 0 & mcc = 460 & lang = 1 & appid = 888lt = 1 HTTP/1.1 "200 2 "-""-"
It can be seen that the service name is/comum, and then the cut command is used to separate the service with "/" and comum is separated.
Cat 03-23.log | awk '{print $7}' | cut-d/-f 2 | sort | uniq-c | sort-n | head-n 1
Sort is sorting, sort-n is ascending, sort-nr is descending, and uniq is removing duplicate values.
7. Use the find command to delete log files
When a program generates a large number of log files, but it cannot be completely deleted, you can use the find command to find the logs and delete the logs with xargs, saving time and eliminating the need to delete the logs one by one.
Find./-type f! -Newer server. log.11 | grep server. log | xargs rm-rf {};
. /Indicates the current directory. Remember to use grep. Otherwise, some other files will be deleted. log.11 is an interval. log.11 old, will be deleted;




The script is being organized. Other scripts will be pasted in the future. Please give more instructions, communicate more, and share more.
You can join the QQ group: 140704742
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.