Most people use grep to find the specified process in the PS aux output when they write the shell filtering process, but they also display the grep process, such as finding the pptpd process, which matches two:
Copy Code code as follows:
[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
A common way to prevent the grep process from appearing is to filter the addition of a pipe grep-v grep:
Copy Code code as follows:
[Root@selboo ~]# ps aux | grep PPTP | Grep-v grep
Root 22054 0.0 0.1 1952 608? Ss Oct22 0:00/usr/sbin/pptpd
A more convenient approach is to use regular grep [p]ptpd to search pptpd this process:
Copy Code code as follows:
[Root@selboo ~]# ps aux | grep [P]PTP
Root 22054 0.0 0.1 1952 608? Ss Oct22 0:00/usr/sbin/pptpd
Although it's not necessarily much more convenient than grep-v grep, however, if you use a large number of loops of monitoring scripts, each time you can reduce the system grep call, that is, reduce the creation process, although the promotion is very small to ignore, but in the shell written by the monitoring script is somewhat improved, Optimization is the attention to detail.
The following five test results were performed:
Using the Grep-v grep method
Copy Code code as follows:
[Root@selboo etc]# time for (I=1 i<=200; i++));d o 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
Use the Regular method
Copy Code code as follows:
[Root@selboo etc]# time for (I=1 i<=200; i++));d o 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