欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

shell脚本杂记(五)

程序员文章站 2022-07-10 09:21:38
...
1,找出多个文件所占用的磁盘空间 du f1 f2
[code="java"]# du hosts pub.sh
4 hosts
4 pub.sh
#
[/code]
默认以字节为单位
显示每个目录的大小du -a dir
[code="java"]# du -a hadoopconf/
8 hadoopconf/mapred-queues.xml.template
4 hadoopconf/httpfs-signature.secret
4 hadoopconf/httpfs-env.sh
4 hadoopconf/mapred-env.sh
4 hadoopconf/mapred-site.xml
4 hadoopconf/core-site.xml
4 hadoopconf/yarn-site.xml
4 hadoopconf/yarn-env.sh
4 hadoopconf/hadoop-env.cmd
4 hadoopconf/hadoop-metrics2.properties
4 hadoopconf/mapred-env.cmd
4 hadoopconf/hdfs-site.xml
4 hadoopconf/hadoop-metrics.properties
4 hadoopconf/configuration.xsl
4 hadoopconf/container-executor.cfg
4 hadoopconf/httpfs-site.xml
4 hadoopconf/slaves
12 hadoopconf/log4j.properties
4 hadoopconf/capacity-scheduler.xml
4 hadoopconf/hadoop-env.sh
4 hadoopconf/httpfs-log4j.properties
12 hadoopconf/hadoop-policy.xml
4 hadoopconf/ssl-client.xml.example
4 hadoopconf/ssl-server.xml.example
4 hadoopconf/yarn-env.cmd
124 hadoopconf/
# [/code]
单独执行du dir
[code="java"]# du hadoopconf/
124 hadoopconf/
# [/code]
如果想采用更好的方式显示,可以使用du -h dir
还可以使用du -c来计算所有文件或目录总共占了多少磁盘空间:
[code="java"]# du -c pub.sh
4 pub.sh
4 总用量
# [/code]
除此之外,统计还支持过滤:
[code="java"]# du -c *.sh
4 begin.sh
4 pub.sh
4 repack.sh
12 总用量
# [/code]

通常我们使用du -sh *来显示当前目录相下,文件大小情况:
[code="java"]# du -sch *
5.4M apache-ant-1.9.4-bin.tar.gz
52M apache-hive-0.13.1-bin.tar.gz
5.0M apache-maven-3.0.5-bin.tar.gz
4.0K begin.sh
8.0K etc
92M hadoop-2.2.0.tar.gz
124K hadoopconf
76M hbase-0.96.2-hadoop2-bin.tar.gz
36K hbaseconf
4.0K hosts
92M jdk-7u25-linux-x64.gz
4.0K pub.sh
4.0K repack.sh
16K zkconf
16M zookeeper-3.4.5.tar.gz
338M 总用量
# [/code]

排除部分文件:
[code="java"]# du --exclude "*.sh" ./
4 ./etc
108 ./hadoopconf
28 ./hbaseconf
16 ./zkconf
345132 ./
# [/code]

指定深度,统计
du --ma-depth 2 dir
df -h 查看磁盘信息
[code="java"]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.7G 2.3G 6.9G 25% /
tmpfs 495M 0 495M 0% /dev/shm
/dev/sda1 194M 28M 157M 15% /boot
/dev/sda3 9.9G 889M 8.6G 10% /home
#[/code]

2,time命令,统计命令脚本耗时
[code="java"]# time ls
apache-ant-1.9.4-bin.tar.gz begin.sh hadoopconf hosts repack.sh
apache-hive-0.13.1-bin.tar.gz etc hbase-0.96.2-hadoop2-bin.tar.gz jdk-7u25-linux-x64.gz zkconf
apache-maven-3.0.5-bin.tar.gz hadoop-2.2.0.tar.gz hbaseconf pub.sh zookeeper-3.4.5.tar.gz

real 0m0.004s
user 0m0.001s
sys 0m0.003s[/code]

3,收集与当前登录用户,启动日志,和启动故障相关的信息
who ,w ,users ,uptime ,last .lastb
[code="java"]# who
root pts/0 2014-09-23 01:48 (192.168.46.24)
root pts/1 2014-09-23 01:13 (192.168.46.24)
#
#
#
#
# w
03:08:48 up 4:59, 2 users, load average: 0.00, 0.00, 0.00
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 192.168.46.24 01:48 0.00s 0.13s 0.00s w
root pts/1 192.168.46.24 01:13 37:06 0.03s 0.03s -bash
#
#
#
#
# users
root root
#
#
#
#
# uptime
03:08:57 up 4:59, 2 users, load average: 0.00, 0.00, 0.00
#
#
#
#
# last
root pts/0 192.168.46.24 Tue Sep 23 01:48 still logged in
root pts/0 192.168.46.24 Tue Sep 23 01:30 - 01:48 (00:17)
root pts/0 192.168.46.24 Tue Sep 23 01:17 - 01:30 (00:13)
root pts/0 192.168.46.24 Tue Sep 23 01:17 - 01:17 (00:00)
root pts/0 192.168.46.24 Tue Sep 23 01:16 - 01:17 (00:00)
root pts/1 192.168.46.24 Tue Sep 23 01:13 still logged in
root pts/0 192.168.46.24 Tue Sep 23 01:00 - 01:16 (00:15)
root pts/0 192.168.46.24 Tue Sep 23 00:59 - 01:00 (00:00)
dong pts/1 zk1 Mon Sep 22 22:34 - 22:35 (00:00)
root pts/0 192.168.46.24 Mon Sep 22 22:16 - 23:38 (01:21)
root tty1 Mon Sep 22 22:10 - 23:10 (01:00)
reboot system boot 2.6.32-431.el6.x Mon Sep 22 22:09 - 03:09 (04:59)
root pts/0 192.168.46.19 Wed Jun 11 00:12 - crash (103+21:56)
root tty1 Wed Jun 11 00:12 - crash (103+21:57)
reboot system boot 2.6.32-431.el6.x Wed Jun 11 00:12 - 03:09 (104+02:56)
root pts/3 192.168.46.16 Fri May 30 17:19 - crash (11+06:53)
root pts/2 192.168.46.16 Fri May 30 17:01 - crash (11+07:10)
root pts/3 192.168.46.16 Fri May 30 16:19 - 17:19 (01:00)
root pts/2 192.168.46.16 Fri May 30 16:01 - 17:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 15:19 - 16:19 (01:00)
root pts/2 192.168.46.16 Fri May 30 15:01 - 16:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 14:19 - 15:19 (01:00)
root pts/2 192.168.46.16 Fri May 30 14:01 - 15:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 13:18 - 14:18 (01:00)
root pts/2 192.168.46.16 Fri May 30 13:01 - 14:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 12:18 - 13:18 (01:00)
root pts/2 192.168.46.16 Fri May 30 12:01 - 13:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 11:18 - 12:18 (01:00)
root pts/2 192.168.46.16 Fri May 30 11:01 - 12:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 10:18 - 11:18 (01:00)
root pts/2 192.168.46.16 Fri May 30 10:01 - 11:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 09:18 - 10:18 (01:00)
root pts/2 192.168.46.16 Fri May 30 09:01 - 10:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 08:18 - 09:18 (01:00)
root pts/2 192.168.46.16 Fri May 30 08:01 - 09:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 07:18 - 08:18 (01:00)
root pts/2 192.168.46.16 Fri May 30 07:01 - 08:01 (01:00)
root pts/3 192.168.46.16 Fri May 30 06:18 - 07:18 (01:00)
root pts/2 192.168.46.16 Fri May 30 06:01 - 07:01 (01:00)
root pts/2 192.168.46.16 Fri May 30 04:26 - 06:00 (01:34)
root pts/1 192.168.46.16 Fri May 30 04:00 - crash (11+20:11)
root tty1 Fri May 30 03:59 - 04:59 (01:00)
root pts/0 192.168.46.16 Fri May 30 03:52 - 06:18 (02:26)
root pts/0 192.168.46.16 Thu May 29 18:34 - 03:48 (09:13)
reboot system boot 2.6.32-431.el6.x Thu May 29 18:33 - 03:09 (116+08:35)
root pts/3 192.168.46.16 Thu May 29 06:01 - crash (12:31)
root pts/2 192.168.46.16 Thu May 29 05:56 - crash (12:37)
root pts/1 192.168.46.16 Thu May 29 05:56 - crash (12:37)
root pts/0 192.168.46.16 Thu May 29 05:55 - crash (12:38)
reboot system boot 2.6.32-431.el6.x Thu May 29 05:54 - 03:09 (116+21:14)
root pts/6 192.168.46.16 Thu May 29 05:48 - down (00:05)
root pts/5 192.168.46.16 Thu May 29 05:46 - down (00:07)
root pts/5 192.168.46.16 Thu May 29 05:46 - 05:46 (00:00)
root pts/5 192.168.46.16 Thu May 29 05:41 - 05:46 (00:04)
root pts/3 192.168.46.16 Thu May 29 05:39 - down (00:14)
root pts/2 192.168.46.16 Thu May 29 05:38 - down (00:15)
root pts/2 192.168.46.16 Thu May 29 05:37 - 05:38 (00:00)
root pts/1 192.168.46.16 Thu May 29 05:37 - down (00:16)
root pts/0 192.168.46.18 Thu May 29 04:43 - down (01:10)
root pts/5 192.168.46.18 Thu May 29 03:42 - 04:43 (01:00)
(00:03)
root pts/4 master Fri Mar 7 01:16 - down (00:13)
root pts/3 192.168.211.253 Fri Mar 7 01:05 - down (00:25)
root pts/2 localhost Fri Mar 7 01:04 - down (00:25)
root pts/1 localhost Fri Mar 7 01:04 - down (00:25)
root pts/0 192.168.211.36 Thu Mar 6 22:46 - down (02:43)
root tty1 Thu Mar 6 22:46 - down (02:44)
reboot system boot 2.6.32-431.el6.x Thu Mar 6 22:45 - 01:30 (02:44)
root tty1 Thu Mar 6 22:32 - crash (00:12)
reboot system boot 2.6.32-431.el6.x Thu Mar 6 22:32 - 01:30 (02:58)
root tty1 Thu Mar 6 22:05 - crash (00:26)
reboot system boot 2.6.32-431.el6.x Thu Mar 6 22:04 - 01:30 (03:25)
root tty1 Thu Mar 6 21:58 - down (00:06)
reboot system boot 2.6.32-431.el6.x Thu Mar 6 21:58 - 22:04 (00:06)
root tty1 Thu Mar 6 21:53 - crash (00:04)
reboot system boot 2.6.32-431.el6.x Thu Mar 6 21:52 - 22:04 (00:11)
root tty1 Thu Mar 6 21:50 - crash (00:02)
reboot system boot 2.6.32-431.el6.x Thu Mar 6 21:49 - 22:04 (00:14)

wtmp begins Thu Mar 6 21:49:33 2014
#
#
#
# lastb
root ssh:notty 192.168.46.18 Thu May 29 03:35 - 03:35 (00:00)
root ssh:notty 192.168.75.1 Thu May 29 02:03 - 02:03 (00:00)
root ssh:notty 192.168.75.1 Thu May 29 02:03 - 02:03 (00:00)

btmp begins Thu May 29 02:0[/code]

last USER获取最后登录的用户
last reboot最后重启会话信息
lastb 获取失败用户登录会话信息


3,使用watch命令,监控目录情况

[code="java"]# watch 'ls -l | grep "^d" '
Every 2.0s: ls -l | grep "^d" Tue Sep 23 03:15:59 2014

drwxr-xr-x 2 root root 4096 9月 23 01:02 etc
drwxr-xr-x 2 root root 4096 9月 22 16:58 hadoopconf
drwxr-xr-x 2 root root 4096 9月 22 16:58 hbaseconf
drwxr-xr-x 2 root root 4096 9月 22 16:58 zkconf

[/code]

默认是2秒,我们也可以指定时间
[code="java"]# watch -n 4 'ls -l'
Every 4.0s: ls -l Tue Sep 23 03:18:04 2014

总用量 344992
-rw-r--r-- 1 root root 5564593 6月 9 16:00 apache-ant-1.9.4-bin.tar.gz
-rw-r--r-- 1 root root 54246778 7月 24 17:22 apache-hive-0.13.1-bin.tar.gz
-rw-r--r-- 1 root root 5144659 7月 10 12:50 apache-maven-3.0.5-bin.tar.gz
-rw-r--r-- 1 root root 3693 9月 23 02:18 begin.sh
drwxr-xr-x 2 root root 4096 9月 23 01:02 etc
-rw-r--r-- 1 root root 96183833 6月 9 17:27 hadoop-2.2.0.tar.gz
drwxr-xr-x 2 root root 4096 9月 22 16:58 hadoopconf
-rw-r--r-- 1 root root 79367504 7月 14 10:55 hbase-0.96.2-hadoop2-bin.tar.gz
drwxr-xr-x 2 root root 4096 9月 22 16:58 hbaseconf
-rw-r--r-- 1 root root 42 9月 23 02:07 hosts
-rw-r--r-- 1 root root 96316511 11月 20 2013 jdk-7u25-linux-x64.gz
-rw-r--r-- 1 root root 235 9月 23 02:30 pub.sh
-rw-r--r-- 1 root root 95 9月 23 02:18 repack.sh
drwxr-xr-x 2 root root 4096 9月 22 16:58 zkconf
-rw-r--r-- 1 root root 16402010 6月 12 13:54 zookeeper-3.4.5.tar.gz[/code]
突出差异,watch -d 'cmd'
4,使用logrotate来管理日志文件,可以像JAVA里面的log4j一样,滚动生成日志文件,

logrorate的配置目录在/et/logrotate.d里面
我们可以定义一个日志如下所示:
/var/log/yum.log {
missingok
notifempty
size 30k
yearly
create 0600 root root
}


下面看下如何向系统日志文件,写入日志信息:
[code="java"]# logger LOG_MESSAGE
# logger 你好啊
# tail -n 1 /var/log/messages
Sep 23 03:25:17 master root: 你好啊
# [/code]

加一个特定的标记:
[code="java"]# logger -t "==>" 我我我我
# tail -n 1 /var/log/messages
Sep 23 03:27:22 master ==>: 我我我我
#
[/code]

5,查看电源测量:
安装yum install powertop
PowerTOP 2.3      Overview   Idle stats   Frequency stats   Device stats   Tunables                                     

Summary: 5.9 wakeups/second, 0.0 GPU ops/seconds, 0.0 VFS ops/sec and 5.9% CPU use

Usage Events/s Category Description
44.7 ms/s 0.00 Process [khugepaged]
8.0 ms/s 1.0 Process powertop
1.8 ms/s 2.0 Process [events/0]
3.1 ms/s 1.0 Process [kswapd0]
10.3 µs/s 1.0 Process [vmmemctl]
5.5 µs/s 1.0 Process [flush-8:0]
464.4 µs/s 0.00 Timer rh_timer_func
216.5 µs/s 0.00 Timer tick_sched_timer
187.0 µs/s 0.00 Process sshd: [email protected]/0
137.0 µs/s 0.00 Interrupt [9] RCU(softirq)
94.6 µs/s 0.00 Interrupt [3] net_rx(softirq)
93.3 µs/s 0.00 Interrupt [19] eth0
25.5 µs/s 0.00 Timer delayed_work_timer_fn
24.7 µs/s 0.00 Interrupt [1] timer(softirq)
22.6 µs/s 0.00 Process [mpt_poll_0]
16.8 µs/s 0.00 Timer hrtimer_wakeup
16.4 µs/s 0.00 Timer process_timeout
9.8 µs/s 0.00 Process [ksoftirqd/0]
2.6 µs/s 0.00 Timer ipmi_timeout
2.6 µs/s 0.00 Timer tcp_write_timer





6,监控磁盘的io
使用iotop
iotop -o 显示正在进行的io活动的进程
iotop -b -n 2打印两次统计数据
iotop -p pid 显示需要监视的pid进程的io情况

7,检查磁盘及文件系统错误
fsck /dev/sdb3检查指定的路径
fsck -A 检查所有的文件系统
fsck -a /dev/sda2 自动修复错误
fsck -AN 模拟fsck执行的操作

8,which命令查找某个命令的位置:
[code="java"]# which java
/usr/local/jdk/bin/java
#[/code]
9,whereis 命令找出源码的路径,如果有的话:
[code="java"]# whereis java
java: /etc/java /usr/lib/java /usr/share/java
#

[/code]

10,file命令,来确定文件类型:
[code="java"]# file bb.sh
bb.sh: UTF-8 Unicode text
#
[/code]


11,whatis 输出简短的描述信息:
12,kill命令
列出所有信号:
[code="java"]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
#

[/code]
kill 9 pid 最常用的终止进程命令
killall 进程名,终止一组相关的进程


13,trap命令,可以捕捉信号,并执行新的命令
14,wall命令,同一个终端广播信息,mesg y 或者mesg n可以禁止或允许写入信息
15,采集系统信息:
[code="java"]# hostname
master
# uname -n
master
# unmae -a
-bash: unmae: command not found
# uname -a
Linux master 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
# uname -r
2.6.32-431.el6.x86_64
# uname -m
x86_64
# cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 58
model name : Intel(R) Core(TM) i5-3470 CPU @ 3.20GHz
stepping : 9
cpu MHz : 3192.789
cache size : 6144 KB
fpu : yes
fpu_exception : yes
cpuid level : 13
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc up arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt aes xsave avx f16c rdrand hypervisor lahf_lm ida arat epb xsaveopt pln pts dts fsgsbase smep
bogomips : 6385.57
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management:

# cat /proc/meminfo
MemTotal: 1012352 kB
MemFree: 242180 kB
Buffers: 19320 kB
Cached: 670172 kB
SwapCached: 0 kB
Active: 360848 kB
Inactive: 335756 kB
Active(anon): 7124 kB
Inactive(anon): 212 kB
Active(file): 353724 kB
Inactive(file): 335544 kB
Unevictable: 0 kB
Mlocked: 0 kB
SwapTotal: 0 kB
SwapFree: 0 kB
Dirty: 16 kB
Writeback: 0 kB
AnonPages: 7128 kB
Mapped: 5132 kB
Shmem: 224 kB
Slab: 57144 kB
SReclaimable: 35492 kB
SUnreclaim: 21652 kB
KernelStack: 592 kB
PageTables: 1308 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 506176 kB
Committed_AS: 48544 kB
VmallocTotal: 34359738367 kB
VmallocUsed: 144700 kB
VmallocChunk: 34359588136 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 8192 kB
DirectMap2M: 1040384 kB
# cat /proc/partitions
major minor #blocks name

8 0 20971520 sda
8 1 204800 sda1
8 2 10240000 sda2
8 3 10525696 sda3
# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0000791d

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 1301 10240000 83 Linux
/dev/sda3 1301 2611 10525696 83 Linux
# lshw
-bash: lshw: command not found
# ls
ls lsattr lsblk lscpu lsinitrd lsmod
# ls
ls lsattr lsblk lscpu lsinitrd lsmod
# lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 1
On-line CPU(s) list: 0
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 58
Stepping: 9
CPU MHz: 3192.789
BogoMIPS: 6385.57
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 6144K
NUMA node0 CPU(s): 0
#[/code]
相关标签: 脚本 shell