Commonly used Linux command snippets

1. View logged in users  
$ who -a | grep -Ev "tty|system boot|run-level"
2. List the top five processes currently occupying the CPU  
$ ps aux | sort -k3,3nr | head -5  
3. List the top five process information currently occupying memory  
$ ps aux | sort -k4,4nr | head -5  
4. Dynamically monitor command execution  
$ watch -n 1 -d "#监控的命令"  
5. Query the process information of the currently occupied device    
$ fuser  
6. Find files 7 days ago for batch deletion  
$ find . -type f -mtime +7 | xargs rm -f  
7. Kill process by process name  
ps aux | grep ${PNAME} | grep -v grep | cut -c 9-15 | xargs kill -9  
8. Print GC information  
$JAVA_HOME/bin/jstat -gcutil  PID  1000 100  
9. View the status information of the heap memory and export the memory snapshot

```bash  
$ $JAVA_HOME/bin/jmap -heap ${PID}  
$ $JAVA_HOME/bin/jmap -dump:format=b,file=edwfms-8084_108.bin  $PID  

##### 10. find使用  
```bash
$ find . -size -200k -type f | xargs -I {}  cp -rp {}  /tmp/history_file/

Explanation: -I assigns each parameter of xargs to {} line by line, you can use {} instead -size -200k file is smaller than 200K  

11. View the local public network IP

```bash  
$ curl ifconfig.me 

##### 12.删除空目录

```bash  
$ find . -empty -type d | xargs rmdir  
13.JAVA_OPTS configuration 
JAVA_OPTS="-Xms1024m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=256m"  
14. View the number of network connections

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


网络连接状态统计: netstat

分析解决网络连接问题和检查接口/端口统计数据、路由表、协议状态等等的

**语法:**

netstat -l 显示所有处于监听状态的端口列表
netstat -a 显示所有端口;如果去指定仅显示 TCP 端口,使用 -at(指定信显示 UDP 端口,使用 -au)
netstat -r 显示路由表
netstat -s 显示每个协议的状态总结
netstat -i 显示每个接口传输/接收(TX/RX)包的统计数据

centos7 默认不带该命令,需要手动 安装

```bash
$ yum install net-tools

View the number of system connections

$ netstat -n | awk '/^tcp/ {n=split($(NF-1),array,":");if(n<=2)++S[array[(1)]];else++S[array[(4)]];++s[$NF];++N} \
END {for(a in S){printf("%-20s %s\n", a, S[a]);++I}printf("%-20s %s\n","TOTAL_IP",I);for(a in s) printf("%-20s %s\n",a, s[a]);printf("%-20s %s\n","TOTAL_LINK",N);}'

--System parameter TCP identification
CLOSED: no connection is active or in progress
LISTEN: the server is waiting for an incoming call
SYN_RECV: a connection request has arrived, waiting for confirmation
SYN_SENT: the application has started, open a connection
ESTABLISHED: normal data transmission status
FIN_WAIT1: The application says it has completed
FIN_WAIT2: The other side has agreed to release
CLOSING: Both sides try to close at the same time
TIME_WAIT: The other side has initiated a release. (Data connection has been connected)
LAST_ACK: Wait for all packets to die. View the number of concurrent requests of Apache and its TCP connection status

15. Monitor interrupt status 

```bash 
$ watch -d  cat /proc/softirqs 

##### 16.强制卸载NFS设备
```bash
$ mount -l /nfs挂载目录
17. File occupancy statistics

1) Analyze from the root directory and use the following command to find the secondary directory with the largest proportion

$ cd /
#第一种命令
$ find / -maxdepth 1 | grep -vE 'proc|lost|upload|cgroup' | sed  's#/##g' | awk '{print $0}' | xargs du -a --max-depth=0 | sort -nr | awk '{print $2}' | xargs du -sh

#第二种方法
$ find / -maxdepth 1 ! -path /  | grep -vE 'proc|lost|upload|cgroup' | awk '{print $0}' | xargs du -a --max-depth=0 |  sort -nr | awk '{print $2}' | xargs du -sh
18. Move image files of the same file type to the image_dir directory
$ ls | grep -E 'jpg|png' | xargs -I {} mv {} image_dir/
19. Count the number of physical CPUs
$ cat /proc/cpuinfo | grep "core id" | awk -F ':' '{print $2}' | sort -rn | uniq  | wc -l
20. Check Beijing time from the command line
$ curl http://quan.suning.com/getSysTime.do
21.sed replacement line
#整行处理模式
$ sed -i 's/要被取代的字串/新的字串/g' 文件名 

#参数 -i 表示替换,如果不加 -i 则表示只预览替换是否正确,不实际修改文件
22. Count the number of files
$ find . -type f | wc -l
23. Ubuntu view release version
$ lsb_release -a
24. Remove the user from the group
$ sudo gpasswd -d root ubuntu
Removing user root from group ubuntu
25. fuser
#查询当前占用设备的进程信息&剔除进程
$ fuser -m -v /media/USB/ 
$ fuser -m -v -i -k /media/USB/
26. kill
#手动过滤出进程号再Kill掉
ps aux | grep send_jvm.sh | grep -v grep | cut -c 9-15 | xargs kill -9
9-15 是进程ID所在的列位置xargs 将结果解析成参数

#过滤出进程号再
kill pgrep httpd | xargs kill -9
27.nc
#!/bin/bash 
ip=$1 #服务器IP 
port=$2 #服务器端口 
while : 
do 
echo -e "$(date +%H:%M:%S)--$(nc -v -w 5 -z $1 $2)" 
sleep 1 
done #-w 5 表示超时时间
29.ps
#显示消耗内存/CPU最多的10个进程
$ ps aux | sort -nk +4 | tail -10 ps aux | sort -nk +3 | tail -10
#使用ps 查看进程使用的环境变量信息
$ ps -eo pid,cmd e | tail -n 1
30.taskset

taskset is used to view and set the "CPU affinity". To put it bluntly, it is to view or configure the binding relationship between the process and the cpu, and let a process run on the specified CPU core, which is the "binding core"

0) Applicable scenarios

Redis process running binding

1) Display the CPU on which the process is running

taskset -p pid
Note that this command returns hexadecimal. After converting to binary, each bit corresponds to a logical CPU, and the low bit is CPU No. 0, and so on. If each position is 1, it means that the process is bound to the CPU. For example, 0101 means that the process is bound to logical CPUs 0 and 3

2) Binding core settings

taskset -pc 3 pid means to bind the process pid to the third core (note: do not make 3 into binary) taskset -c 3 command means to execute the command command and bind the process started by command to the third core Nuclear on.

31.ulimit

Background
When using the property OA grguser user to log in to its production OA application server, the execution command error is as follows
-bash: fork: retry: Resource temporarily unavailable

Troubleshoot
# prompt root user execute $ prompt grguser user execute
1) View the number of processes/threads run by the current user
#lsof -u grguser | wc -l
1450
2) View the maximum number of processes and threads that the current user can run, if this If the value is less than the number of running processes/threads in the current terminal window, an error "-bash: fork: retry: Resource temporarily unavailable" will be reported that resources cannot be allocated

# cat /etc/security/limits.d/90-nproc.conf
*          soft    nproc     1024 #系统安装时默认是1024
root       soft    nproc     unlimited
$ ulimit -a
max user processes              (-u) 1024

solve

root用户下修改/etc/security/limits.d/90-nproc.conf文件,将nproc的值调大 ,调整为8192

$ulimit -a
max user processes              (-u) 8192

如果没有发生改变,最好重启下服务器

Note that the general search results all say that the file /etc/security/limits.conf should be changed. In fact, after I modified it, I found that it still did not take effect. Finally, I modified /etc/security/limits.d/90-nproc.conf. Take effect after file

32.vmstat

View specific vmstat documents

33starce

File commands opened by the tracking program

$ strace -e open netstat #跟踪netstat命令打开的文件数
34. lsof

List files opened by the current process

lsof abc.txt 显示开启文件abc.txt的进程
lsof -i :22 知道22端口现在运行什么程序
lsof -c abc 显示abc进程现在打开的文件
lsof -p 12 看进程号为12的进程打开了哪些文件
35. Traffic View

nload
iftop
iptraf
nethogs # This tool can see the traffic situation process
bmon
slurm
tcptrack
vnstat

查看每个连接的实时流量情况的工具
$ pktstat -i eth3 -nt
36. rsync

View specific rsync documents

37. grep

Search by file content, xxx is the file content,'.' is the search range, in the current directory

$ grep -rin xxx .

Guess you like

Origin blog.51cto.com/4073279/2678290