Linux Shell Advanced Programming Tips 2----shell tools

Source: Internet
Author: User

2.shell Tools
2.1. log files
Brief introduction
It is important to create a log file, which records important information. In the event of an error, this information is very useful for our troubleshooting, and the monitoring information can be logged to the log file.
Common methods of log files
Log files identified by time
Example

#!/bin/bash# the current date current_date= ' date ' +%y%m%d ' #今天的日志文件名todaylog = ' log/${current_date}.log ' #如果日志文件不存在, create an IF [!-F $ Todaylog]then    Touch $todaylogfi # Output log to log file log_time_format= ' date ' +%y-%m-%d%T ' ' Echo ' ${log_time_format} command Start " >> $todaylog # #command blockssleep 4# #输出日志到日志文件log_time_format = ' Date ' +%y-%m-%d%T ' echo ' ${log_time_format} Command End ">> $todaylog

Temporary files that are identified by the process number
Example

#!/bin/bash# get current process number current_pid=$$ #获得特定进程的进程号并重定向到一个临时文件中ps-aux | grep "/USER/SBIN/HTTPD" | Grep-v "grep" | awk ' {print $} ' >/tmp/${current_pid}.txt# command block start for PID in ' cat/tmp/${current_pid}.txt ' do{    echo ' kill-9 $pid 
   
    kill-9 $pid}done# command block end # Delete temporary files rm-f/tmp/${current_pid}.txt
   

2.2. Signal
Brief introduction
A signal is a message sent by the system to a script or command informing them of the occurrence of an event.
Kill command
Kill-l list all the signals.
List some common signals
1 SIGHUP hangs or parent process is killed
2 SIGINT interrupt signal from the keyboard, usually ctrl-c
3 Sigquit exit from keyboard
9 SIGKILL Unconditional Exit
One SIGSEGV segment (memory) conflict
SIGTERM software termination (default Kill process)
Signal 0 is the "exit shell" signal. To signal 0, simply type exit from the command line, or use ctrl-d on a process or command line
Kill sends a signal to the process
Example
Kill-s Signal Name Process number This format to send the appropriate signal to the process
Kill-s SIGKILL 7696 kills process with process number 7696
Kill-the number of the signal process number in this format to send the corresponding signal to the process
Kill-9 7696 kills a process with process number 7696
2.3.trap Capture Signal
Brief introduction
Signals can be captured by the application or script and take action based on the process number (1, 2, 3, and 15). Some signals cannot be captured. For example, if a command receives a signal of 9, it can no longer capture other signals
After snapping to a signal, it may take one of the following three actions
1. No action is taken by the system for processing
2. Capture the signal, but ignore it
3. Capture the signal and take action accordingly
Traps allow you to capture signals in the script. The command format is: Trap name signal (s)
Where name is the sequence of actions taken after capturing the signal. In practice, name is typically a function that is specifically designed to handle the captured signal. Name needs to be enclosed in double quotes "".
Signal is the signal to be captured.
The most common action is
1. Clear Temporary files
2. Ignore the signal example: Trap "" 2 3
3. Ask the user whether to terminate the operation of the script
Example 1

#!/bin/bash# Capture Signal 2, if captured to execute Exitprocesstrap "ExitProcess" 2loop=0function exitprocess () {    echo "you just hit < Ctrl-c>, at number $LOOP "    echo" I'll now Exit "    exit 1}while:do    loop=$[$LOOP +1]    echo $LOOP    Sleep 1done

Example 2

#!/bin/bashloop=0trap "ExitProcess" 2hold1=/tmp/hold1.$ $HOLD 2=/tmp/hold2.$ $function exitprocess () {    echo-e "\ Nrecived Interrupt .... "    echo-n" Do you really wish to exit? ( y/n) "    read ANS case    $ANS in    y|y)        rm_tmp_file        ;;    N|n)        ;;    *)        exitprocess        ;;    Esac}function Rm_tmp_file () {    echo "<CTRL-c> detected ... Now cleaning ... Wait "    rm/tmp/*.$$ 2>/dev/null    exit 1}while:d o    loop=$[$LOOP +1]    echo $LOOP    DF >> $ HOLD1    ps-xa >> $HOLD 2    sleep 1done

2.4.eval
Brief introduction
The eval command will first scan the command line for all permutations before executing the command. This command applies to variables that scan for their functionality
example, the contents of the MyFile file are ls-l
Myfile= "Cat MYFILE"; ' eval $MYFILE '
Equivalent to eval ' cat myfile '
That is, the command that executes the cat myfile output.
2.5.logger
Brief introduction:
Logger command to send a message to the/var/log/messages file
The general form of the Logger command is:
Logger-p-I. Message
-P is the priority, here only the priority to prompt the user's attention, which is also the default value
-I record the process number of the sending message in each message
Example
Logger-p-i "Chinaitlab Shenzhen"
View/var/log/messages to see the information that was inserted

Linux Shell Advanced Programming Tips 2----shell tools

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.