编程知识 cdmana.com

Common commands of Linux operation and maintenance engineers (Collection)

1、 Delete 0 Byte file

find -type f -size 0 -exec rm -rf {} ;

2、 Check the process

Sort by memory from large to small

PS -e -o "%C : %p : %z : %a"|sort -k5 -nr

3、 Press CPU The utilization is arranged from large to small

ps -e -o "%C : %p : %z : %a"|sort -nr

4、 Print cache Inside URL

grep -r -a jpg /data/cache/* | strings | grep "http:" | awk -F'http:' '{print "http:"$2;}'

5、 see http The number of concurrent requests and TCP Connection status :

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

6、sed In this article Root A line , matching Root a line , take no Replace with yes

sed -i '/Root/s/no/yes/' /etc/ssh/sshd_config 

7、 How to kill MySQL process

ps aux |grep mysql |grep -v grep  |awk '{print $2}' |xargs kill -9 
( Learn from it awk Use of )

killall -TERM mysqld

kill -9 `cat /usr/local/apache2/logs/httpd.pid`   # Try the killing process PID

8、 Show run 3 Level enabled services :

ls /etc/rc3.d/S* |cut -c 15-   ( Learn from it cut Use of , Intercept data )

9、 How to write SHELL Show multiple messages , use EOF

cat << EOF
+--------------------------------------------------------------+
|       === Welcome to Tunoff services ===                  |
+--------------------------------------------------------------+
EOF

10、for The ingenious use of ( If given MySQL Build soft links )

cd /usr/local/mysql/bin
for i in *
do ln /usr/local/mysql/bin/$i /usr/bin/$i
done

11、 take IP Address

ifconfig eth0 |grep "inet addr:" |awk '{print $2}'| cut -c 6-  

perhaps

ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'

12、 Memory size :

free -m |grep "Mem" | awk '{print $2}'
13

netstat -an -t | grep ":80" | grep ESTABLISHED | awk '{printf "%s %sn",$5,$6}' | sort

14、 see Apache The number of concurrent requests and TCP Connection status :

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

15、 because IT Way station Colleagues want to make statistics of all the servers jpg The size of the file , Wrote a SHELL Give him statistics . Original use xargs Realization , But he deals with part of it at a time . There are many sums of things ……, The following command will solve the problem .

find / -name *.jpg -exec wc -c {} ;|awk '{print $1}'|awk '{a+=$1}END{print a}'

CPU The number of ( More accounting and more CPU,cat /proc/cpuinfo |grep -c processor) The more , The lower the system load , The more requests can be processed per second .

16、CPU load

cat /proc/loadavg

Check that the first three outputs exceed the system logic CPU Of 4 times .

17、CPU load

mpstat 1 1

Check %idle Is it too low ( For example, less than 5%).

18、 Memory space

free

Check free Is the value too low , It can also be used. # cat /proc/meminfo

19、SWAP Space

free

Check swap used Is the value too high , If swap used It's too high , Further inspection swap Do you move frequently :

vmstat 1 5

Observe si and so Is the value larger

20、 disk space

df -h

Check for partition usage (Use%) Too high ( For example, over 90%) Close to, as of, a partition , You can enter the mount point of the partition , Use the following command to find the most occupied file or directory :

du -cks * | sort -rn | head -n 10

21、 disk I/O load

iostat -x 1 2

Check I/O Usage rate (%util) Is it more than 100%

22、 Network load

sar -n DEV

Check network traffic (rxbyt/s, txbyt/s) Is it too high

23、 Network error

netstat -i

Check for network errors (drop fifo colls carrier), You can also use commands :# cat /proc/net/dev

24、 Number of network connections

netstat -an | grep -E “^(tcp)” | cut -c 68- | sort | uniq -c | sort -n

25、 Total processes

ps aux | wc -l

Check whether the number of processes is normal ( For example, over 250)

26、 Number of runnable processes

vmwtat 1 5

Column gives the number of runnable processes , Check if it exceeds the system logic CPU Of 4 times

27、 process

top -id 1

Observe if there are abnormal processes .

28、 Network state , Check DNS, Whether the gateway can be connected normally

ping traceroute nslookup dig 

29、 user

who | wc -l

Check if there are too many login users ( For example, over 50 individual ) You can also use commands :# uptime.

30、 system log

# cat /var/log/rflogview/*errors
 Check if there are abnormal error records     You can also search for some unusual keywords , for example :

grep -i error /var/log/messages
grep -i fail /var/log/messages

31、 Core log

dmesg

Check if there are abnormal error records .

32、 system time

date

Check that the system time is correct .

33、 Number of open files

lsof | wc -l

Check if the total number of open files is too large .

34、 journal

# logwatch –print
 To configure  /etc/log.d/logwatch.conf, take  Mailto  Set it to your own  email  Address , start-up  mail  service (sendmail perhaps postfix), So that you can receive daily log reports .
 default  logwatch  Only yesterday's log , It can be used  # logwatch –print –range all  Get all the log analysis results .
 It can be used  # logwatch –print –detail high  Get more specific log analysis results ( Not just error logs ).
 Copy code 

35、 kill 80 Port related process

lsof -i :80|grep -v “ID”|awk ‘{print “kill -9”,$2}’|sh

36、 Clear the dead process

ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9

37、tcpdump Grab the bag , To prevent 80 When the port is attacked, the data can be analyzed

tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts

38、 Then check that the IP And sort them from small to large Be careful “-t +0” There are two spaces in the middle

# less pkts | awk {'printf $3"n"'} | cut -d. -f 1-4 | sort | uniq -c | awk {'printf $1" "$2"n"'} | sort -n -t +0

39、 See how many active php-cgi process

netstat -anp | grep php-cgi | grep ^tcp | wc -l

40、 View the system's self starting Services

chkconfig --list | awk '{if ($5=="3:on") print $1}'

41、kudzu Check the network card model

kudzu --probe --class=network

senior Linux The operation and maintenance engineer must have a common command ( Interview questions )

Common regular expressions

1. Regular expressions matching Chinese characters :[u4e00-u9fa5]

Commentary : It's a headache to match Chinese , With this expression, it's easy

2. Match double byte characters ( Including Chinese characters ):[^x00-xff]

Commentary : Can be used to calculate the length of a string ( A double byte character length meter 2,ASCII Character meter 1)

3. Regular expressions matching blank lines :ns*r

Commentary : Can be used to delete blank lines

4. matching HTML Tagged regular expression :<(S*?)[^>]*>.*?</1>|<.*? />

Commentary : The version circulating on the Internet is too bad , The above one can only match the part , There's nothing we can do about complex nested tags

5. A regular expression that matches the first and last whitespace characters :`^s|s<div

Commentary : Can be used to delete blank characters at the beginning and end of a line ( Including Spaces 、 tabs 、 Page breaks and so on ), Very useful expressions

6. matching Email Regular expression of address :

w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*

Commentary : Form validation is very practical

7. Match URL URL Regular expression of :[a-zA-z]+://[^s]*

Commentary : The function of the version circulated on the Internet is very limited , The above can basically meet the needs

8. Whether matching account number is legal ( Beginning of letter , allow 5-16 byte , Allow alphanumeric underscores ):`^a-zA-Z{4,15}<div

Commentary : Form validation is very practical

9. Match domestic phone number :d{3}-d{8}|d{4}-d{7}

Commentary : The matching form is as follows 0511-4405222 or 021-87888822

10. Matching Tencent QQ Number :[1-9][0-9]{4,}

Commentary : tencent QQ Number from 10000 Start

11. Match China zip code :[1-9]d{5}(?!d)

Commentary : China Post code is 6 Digit number

12. matching ×××:d{15}|d{18}

Commentary : China's ××× by 15 Bit or 18 position

13. matching ip Address :d+.d+.d+.d+

Commentary : extract IP Useful for addresses

14. Match specific numbers :

^[1-9]d*$   // Matching positive integer 
^-[1-9]d*$  // Matching negative integer 
^-?[1-9]d*$  // Matching integer 
^[1-9]d*|0$ // Match non negative integers ( Positive integer  + 0)
^-[1-9]d*|0$  // Match non positive integers ( Negtive integer  + 0)
^[1-9]d*.d*|0.d*[1-9]d*$  // Match positive floating point 
^-([1-9]d*.d*|0.d*[1-9]d*)$ // Match negative float 
^-?([1-9]d*.d*|0.d*[1-9]d*|0?.0+|0)$ // Match floating point 
^[1-9]d*.d*|0.d*[1-9]d*|0?.0+|0$  // Match non negative floating-point numbers ( Positive floating point  + 0)
^(-([1-9]d*.d*|0.d*[1-9]d*))|0?.0+|0$ // Match non positive floating-point numbers ( Negative floating point number  + 0)

Commentary : Useful when dealing with large amounts of data , Pay attention to correction in specific application

15. Match specific string :

^[A-Za-z]+$ // Match by 26 A string of English letters 
^[A-Z]+$ // Match by 26 A string of uppercase letters 
^[a-z]+$ // Match by 26 A string of lowercase letters 
^[A-Za-z0-9]+$ // Match by numbers and 26 A string of English letters 
^w+$ // Match by number 、26 A string of English letters or underscores 

Commentary : The most basic and commonly used expressions

版权声明
本文为[10 years of it migrant workers]所创,转载请带上原文链接,感谢

Scroll to Top