运维-我常用的shell脚本汇总
运维-我常用的shell脚本汇总
根据目录大小排序
某天发现/home分区满了,想知道是哪个目录占了大头,使用该脚本可以帮你完成排序
du --max-depth=1 /home/ | sort -n -r
--max-depth=1
只统计一级目录sort -n -r
按照数字-逆序排序
根据ps结果批量kill进程
ps -ef | egrep 'foo|bar' | grep -v grep | awk '{print $2}'|xargs kill
命令解读:
ps -ef | egrep 'mysql|ssh'
配合 grep
是常见的组合可以全字段搜索
$ps -ef | head
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 2015 ? 00:00:30 /sbin/init
root 2 0 0 2015 ? 00:00:16 [kthreadd]
root 3 2 0 2015 ? 00:08:19 [migration/0]
root 4 2 0 2015 ? 00:07:58 [ksoftirqd/0]
root 5 2 0 2015 ? 00:00:00 [migration/0]
root 6 2 0 2015 ? 00:00:00 [watchdog/0]
root 7 2 0 2015 ? 00:08:01 [migration/1]
root 8 2 0 2015 ? 00:00:00 [migration/1]
root 9 2 0 2015 ? 00:04:57 [ksoftirqd/1]
egrep就是 grep -E
的变体,支持扩展正则表达式。
grep -v grep
也是常见的后缀,目的是过滤grep进程本身的。
awk {'print $2'}
利用awk做列切分,默认按照空白符进行切分,切分后打印出第二列。结合上面的ps
就是选择打印第二列结果。