ahua671 发表于 2018-8-21 13:15:31

Linux 应用技巧shell

  列出正在使用网络的进程
  lsof -P -i -n | cut -f 1 -d " "| uniq | tail -n +2
  用下面的命令找出僵死进程
  ps -A -o stat,ppid,pid,cmd | grep -e '^'
  ps -A -o stat,ppid,pid,cmd | grep -e '^' | awk '{print $2}' | xargs kill -9
  对当前目录的文件按大小从大到小进行排序,不会计算子目录大小
  ls -lSh
  怎样知道某个进程在哪个CPU上运行?
  ps -eo pid,args,psr
  ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
  ps -eo euser,ruser,suser,fuser,f,comm,label
  快速找出发送arp包主机
  tcpdump -l -n arp | grep 'arp who-has' | head -100 | awk '{printf $NF} ' | sort | uniq -c | sort -n
  查找二个文件相同的内容
  grep -Fx -f file1 file2
  sort file1 file2 | uniq -d
  查找file2里 file1里没有的内容
  grep -Fxv -f file1 file2
  查看服务器上一共开了多少的 php-cgi 进程:
  ps -ef |grep "php" | grep -v "grep" | wc -l
  查看已经有多少个php-cgi进程用来处理tcp请求:
  netstat -anop | grep "php" | grep -v "grep" | wc -l
  修改某目录下(含子目录)所有.shell文件为.sh
  rename .shell .sh `find ./ -name *.shell`
  ping整个网段
  for /L %i in (1,1,255) do @ping -a 192.168.0.%i
  过滤secure日志ip
  grep -o '\{1,3\}\.\{1,3\}\.\{1,3\}\.\{1,3\}' /var/log/secure | sort | uniq -c
  杀掉mysql进程:
  ps aux|grep mysql|grep -v grep|awk '{print $2}'|xargs kill -9   (从中了解到awk的用途)
  pgrep mysql |xargs kill -9
  killall -TERM mysqld
  将当前目录文件名全部转换成小写
  for i in *; do mv "$i" "$(echo $i|tr A-Z a-z)"; done
  CPU的数量(多核算多个CPU)
  cat /proc/cpuinfo |grep -c processor
  ps aux | sort -nk +4 | tail
  内存的大小:
  free -m |grep "Mem" | awk '{print $2}'
  查看当前有哪些进程;查看进程打开的文件:
  ps -A ;lsof -p PID
  清除僵死进程。
  ps -eal | awk '{ if ($2 == "Z") {print $4}}' | kill -9
  删除0字节文件
  find -type f -size 0 -exec rm -rf {} \;
  查看进程
  按内存从大到小排列
  ps -e-o "%C: %p : %z : %a"|sort -k5 -nr
  按cpu利用率从大到小排列
  ps -e-o "%C: %p : %z : %a"|sort-nr
  显示运行3级别开启的服务:
  ls /etc/rc3.d/S* |cut -c 15-
  取IP地址:
  ifconfig| grep -Eo \(\{1,3\}[\.]\)\{3\} |sed -n '1p'
  ifconfig eth0|sed -n '2p'|awk '{print $2}'|cut -c 6-30
  ifconfig eth0 | sed -n '/inet /{s/.*addr://;s/ .*//;p}'
  ifconfig | sed -e '/.*inet addr:/!d;s///;s/ .*//'(含127.0.0.1)
  取MAC
  ifconfig | sed -e '/.*HWaddr /!d;s///;'
  ifconfig | awk '/HWaddr/ { print $NF }'
  用以下命令找出占用空间最多的文件或目录
  du -cks * | sort -rn | head -n 10
  进程top -id 1
  观察是否有异常进程出现
  杀掉80端口相关的进程
  lsof -i :80|grep -v "PID"|awk '{print "kill -9",$2}'|sh
  tcpdump 抓包 ,用来防止80端口被人***时可以分析数据
  tcpdump -c 10000 -i eth0 -n dst port 80 > /root/pkts
  怎样知道某个进程在哪个CPU上运行?
  ps -eo pid,args,psr
  查看硬件制造商
  dmidecode -s system-product-name
  清除所有arp缓存
  arp -n|awk '/^/{system("arp -d "$1)}'
  其实Linux也有内部命令清除所有arp缓存,但是不太好记忆,用的人很少。以下命令清除eth0接口的所有arp缓存。
  ip neigh flushdev eth0
  绑定已知机器的arp地址
  cat /proc/net/arp | awk '{print $1 " " $4}' |sort -t. -n +3 -4 > /etc/ethers
  按内存从大到小排列进程:
  ps -eo "%C : %p : %z : %a"|sort -k5 -nr
  清空linux buffer cache
  sync && echo 3 > /proc/sys/vm/drop_caches
  清除所有arp缓存
  arp -n|awk '/^/ {print "arp -d "$1}'|sh
  将当前目录文件名全部转换成小写
  for i in *; do mv "$i" "$(echo $i|tr A-Z a-z)"; done
  显示当前目录下文件的大小
  du -hs $(du -sk ./`ls -F |grep /` |sort -nr |awk '{print $NF}')
  chattr +i filename 禁止删除,chattr -i filename 取消禁止
  lsattr 查看隐藏档属性
  ls -al |grep '^d'   显示目录
  ls -al |grep '^[^d]' 在一个目录中查询不包含目录的所有文件
  shutdown -h +10    系统再过十分钟后自动关机.
  shutdown -r +10 'Hey!Go away!'10分钟后系统重启.
  磁盘I/O负载iostat -x 1 2
  检查I/O使用率(%util)是否超过100%
  网络负载sar -n DEV
  检查网络流量(rxbyt/s, txbyt/s)是否过高
  进程总数   ps aux | wc -l
  检查进程个数是否正常 (比如超过250)
  系统日志cat /var/log/rflogview/*errors
  检查是否有异常错误记录也可以搜寻一些异常关键字,例如:
  grep -i error /var/log/messages
  grep -i 'error|warn' /var/log/messages 查看系统异常
  根据端口列进程
  netstat -ntlp | grep 80 | awk '{print $7}' | cut -d/ -f1
  检查打开文件总数
  lsof | wc -l
  杀掉80端口相关的进程
  lsof -i :80 | grep -v "PID" | awk '{print "kill -9",$2}' | sh

页: [1]
查看完整版本: Linux 应用技巧shell