平常工作使用到的shell命令
1.显示消耗内存/CPU 最 多的 10 个进程
ps aux | sort -nk +4 | tail
ps aux | sort -nk +3 | tail
- 查看 Apache 的 并 发请求数及其 TCP 连 接状态
netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
3.找出 自 己最常用的 10 条命令及使用 次数(或求访问最多的 ip 数)
sed -e ‘s/| /\n/g’ ~/.bash_history |cut -d ‘ ‘ -f 1 | sort | uniq -c | sort -nr | head
##关于这条命令有读者问 sed 语句 的用 处具体是什么,我这里详细说明 下:我们平时用命令是如是用了很多管道符“|”,比如 ps aux | sort -nk +4 | tail /s…/g 就 是 把 管 道 符 “ |” 换 成 换行符” \n” ,于是ps aux | sort -nk +4 | tail
就变成如下:
ps aux | sort -nk +4 | tail
- 日志中第 10 个字段表示连接时间 , 求平均连接时间
cat access_log |grep “connect cbp” |awk‘BEGIN{sum=0;count=0;}{sum+=$10;count++;}END{printf(“sum=%d,count=%d,avg=%f\n”,sum,count,sum/count)}’
5.lsof 命令
lsof abc.txt
#显示开启文件 abc.txt 的进程
lsof -i :22
#知道 22 端口现在运行什么程序
lsof -c abc
#显示 abc 进程现在打开的文件
lsof -p 12
#看进程号为 12 的进程打开了哪些文件
6 .杀掉一个程序的所有进程
pkill -9 httpd
killall -9 httpd
注意尽量不用-9,数据库服务器上更不能轻易 用 kill ,否则 造成重要数据丢失后果将不堪设想。
7.rsync 命令(要求只同 步某天的压缩文件,而且远程目 录保持与本地目 录一致)
/usr/bin/rsync -azvR -passwordfile=/etc/rsync.secrets `find . -name“*$yesterday.gz” -type f `storage@192.168.2.23::logbackup/13.21/
8.把目 录下.sh 文件改名 为.SH
find . -name “*.sh” | sed ’s/\(.*\)\.sh/mv \0 \1.SH/’ |sh
find . -name “*.sh” | sed ’s/\(.*\)\.sh/mv & \1.SH/’|sh
( 跟上面那个效果一样)
9.ssh 执行远程的程序, 并在本地显示
ssh -n -l zouyunhao 192.168.2.14 “ls -al /home/zouyunhao”
- 直接用命令行修改密码
echo “ysPassword” |passwd -stdinys
- 快速生成 ssh 密钥对并上传
ssh-keygen
ssh-copy-id -i ~/.ssh/id_rsa.pub user@remoteServer
13.shell 段注释
:<<’echo hello,world!’
14.查看服务器序列号
dmidecode |grep “Serial Number”
( 查看机器其他硬件信息也可用这个命令)
15.查看网卡是否有网线物理连接
/sbin/mii-tool
16.查看 linux 系统或者 mysql 错误码表示的意思如查看 13 错误码表示的意思:
perror 13
17.关于 cpu 个数查看逻辑 cpu 个数:
cat /proc/cpuinfo | grep “processor” |wc -l
查看物理 cpu 个数:
cat /proc/cpuinfo | grep “physical id”| sort | uniq | wc -l
查看每个物理 cpu 的核数 cores:
cat /proc/cpuinfo | grep “cpu cores”
如 果所有物 理 cpu 的 cores 个数加 起来小于逻辑 cpu 的个数, 则 该 cpu 使用了 超线程技术。
查看每个物理 cpu 中逻辑 cpu 的个数:
cat /proc/cpuinfo | grep “siblings”
18.从格式不规范的日志中截取字符串
perl -ne ’print “$1\n” if /servletPath=(\S+)/g’ test.log