Several easy-to-use process and port Query command combinations and simple scripts (PS netstat awk grep) when developing and debugging Linux services

Source: Internet
Author: User

These days in the construction of Flume service and development in its framework, repeatedly debugging input commands to take the trouble, so here to record some of the tips you find:

First of all, it is a good choice to make a service of your own application, a service needs to support start and Stop,start to launch the program, stop can find the process of START program and turn it off.

Can be changed directly according to the service script under/etc/init.d/, but I have a problem: because those system service scripts are based on the Start-stop-daemon command, this command is very powerful, can start an executable file and the process PID, write to the specified file, This allows the file to be read directly when the process is stopped, shutting down the process.

But I use this command to start their own program, the program started successfully, but the PID file in the absence of any error message is unable to generate, test the root permission to execute, but also test the various write paths (do not need high permissions), all not, so decided to slowly study the problem at the same time, First, the simple way to solve my needs.


The launcher is simple, and as a service, it needs to run in the background, assuming your executable name is called program.

So you need to start:

Nohup program [args] &

To close it requires:

#PROGRAM是进程名

PID = ' pidof program ' if [!-Z ' $PID ']; Then     kill-15 $PID     fi


However, for Java or Python programs, the process is Java or Python, there is no degree of differentiation, so you can not use the "pidof" command, you can only use "ps-ef" command, using grep to filter the process information to find the appropriate process


There can be multiple flume services on a single machine, and each service may have some differences in execution parameters, so use multiple grep:

  Pid= ' Ps-ef | grep java | grep Flume | awk ' {print $} '     if [!-Z ' $PID ']; then     kill-15 $PID     fi

The awk command is used here to remove the value of the second column of PID for the "PS" command to print information.


The start and stop of such a simple service are completed.


I also encounter another requirement, which is to check the occupancy of a port and forcibly close the process that occupies the port:

# Netstat-tlnpa | grep 44444tcp6       0      0 10.58.242.18:44444      :::*                    LISTEN      25049/java

The port query result is like the above format, then we can write the following script according to this format:

pinfo= ' Netstat-tlnpa | grep 44444 ' pid= ' awk ' {split ("' $pinfo '", Array, "/");p rint array[2]} "if [!-Z" $PID "]; Then     kill-15 $PID     fi

It's done.

Digging into awk and grep can be a lot of use, and as a tool for Shell text processing, I'm just a simple everyday application that I hope will help you.

Several easy-to-use process and port Query command combinations and simple scripts (PS netstat awk grep) when developing and debugging Linux services

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.