Tips for Shell grep lookup process _linux Shell

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.