shell grep 查找进程的小技巧
[root@selboo ~]# ps aux | grep pptp
root 20191 0.0 0.2 5108 704 pts/2 R+ 16:58 0:00 grep pptp
root 22054 0.0 0.1 1952 608 ? Ss Oct22 0:00 /usr/sbin/pptpd
常见的防止grep进程出现的方法就是在对加一个管道 grep -v grep 进行过滤:
[root@selboo ~]# ps aux | grep pptp | grep -v grep
root 22054 0.0 0.1 1952 608 ? Ss Oct22 0:00 /usr/sbin/pptpd
还有一个更方便的方法是用 正则 grep [p]ptpd来搜索pptpd这个进程:
[root@selboo ~]# ps aux | grep [p]ptp
root 22054 0.0 0.1 1952 608 ? Ss Oct22 0:00 /usr/sbin/pptpd
虽然说它比 grep -v grep 也未必方便多少,但是如果用大量循环的监控脚本上,每次都能减少一次系统grep调用,也就是减少一次创建进程,虽然提升很小可以忽略不计,但是用在shell写的监控脚本上来说多少还有点提升的,优化就是注重细节嘛。
以下是执行五次测试结果:
使用grep -v grep 方式
[root@selboo etc]# time for (( i=1; i<=200; i++ )) ;do ps aux | grep pptp | grep -v pptp &>/dev/null; done
real 0m1.487s 0m1.475s 0m1.488s 0m1.497s 0m1.499s
user 0m0.335s 0m0.328s 0m0.334s 0m0.326s 0m0.312s
sys 0m0.766s 0m0.757s 0m0.772s 0m0.784s 0m0.795s
使用正则方式
[root@selboo etc]# time for (( i=1; i<=200; i++ )) ;do ps aux | grep [p]ptp &>/dev/null; done
real 0m1.306s 0m1.344s 0m1.303s 0m1.298s 0m1.329s
user 0m0.343s 0m0.313s 0m0.326s 0m0.274s 0m0.322s
sys 0m0.742s 0m0.801s 0m0.753s 0m0.798s 0m0.784s
Linux BASH多进程并行处理的方法实现
#!/bin/bashSEND_THREAD_NUM=13tmp_fifofile="/tmp/$$.fifo"#脚本运行的当前进程ID号作为文件名mkfifo$tmp_fifofile"#新建一个随机fifo管道文件exec6"$tmp_fifofile"#定义文件描述
深入浅出Shell编程 Shell变量介绍
深入浅出Shell编程:Shell变量1系统变量2Shell用户变量■2.1基础■2.2使用技巧■2.3shell中的数组3shell环境变量先不要管Shell的版本,来看看Shell变量,在Shell
linux下监控网络流量的脚本
我看了下,linux下的/proc/net/dev记录了每块网卡发送和接受的包和字节数。因此萌生想法,写了一个。运行效果:[root@74-82-173-217~]#./net.shCurrentIp:inetaddr:74.8