Process and port query command combinations and simple scripts that are easy to use when developing and debugging Linux services

Source: Internet
Author: User

Process and port query command combinations and simple scripts that are easy to use when developing and debugging Linux services

Several useful processes and port query command combinations and simple scripts (ps netstat awk grep) during Linux service development and debugging ).

The flume service has been built and developed under its framework over the past few days. It is annoying to debug and input commands repeatedly. Here are some tips you have found:

First, it is a good choice to make your own application into a service. A service requires support for start and stop, and start can start the program, stop can find the process of the program started by start and turn it off.

Depending on/etc/init. d/, But I encountered a problem: because the system service scripts are based on the start-stop-daemon command, this command is very powerful, you can start an executable file and write the PID of the process to the specified file. In this way, you can directly read the file to stop the process.

However, when I use this command to start my program, the program is successfully started, but the PID file cannot be generated without any error information, and the root permission is tested for execution, I also tested various write paths (without high permissions), but they didn't work. So I decided to solve this problem by using a simple method.

The Startup PROGRAM is very simple. As a service, you need to run it in the background. Assume that your executable file name is PROGRAM.

So you need:

Nohup PROGRAM [args] &

To disable it, you need:

# PROGRAM is the process name

PID = 'pidof PROGRAM'

If [! -Z "$ PID"]; then
Kill-15 $ PID
Fi

However, for java or python programs, the process is java or python and there is no discrimination. Therefore, the "pidof" command cannot be used, so the "ps-ef" command can only be used, use grep to filter process information to find a proper process.

One machine can have multiple flume services, and each service may execute different parameters. Therefore, multiple grep are required:


PID = 'ps-ef | grep java | grep flume | awk '{print $2 }''
If [! -Z "$ PID"]; then
Kill-15 $ PID
Fi
The awk command is used here to retrieve the PID value of the second column of the "ps" command printing information.

In this way, start and stop of a simple service are completed.

I also encountered another requirement: to check the occupation of a port and forcibly disable the process that occupies the Port:

# Netstat-tlnpa | grep 44444
Tcp6 0 0 10.58.242.18: 44444: * LISTEN 25049/java
The port query result is in the preceding format. You can write the following script based on this format:

Pinfo = 'netstat-tlnpa | grep 44444'

PID = 'awk' {split ("'$ pinfo'", array, "/"); print array [2]}''

If [! -Z "$ PID"]; then
Kill-15 $ PID
Fi

That's it.

In-depth mining of awk and grep can also be of a lot of use. As a powerful tool for shell text processing, I am here only a simple daily application and hope to help you.

This article permanently updates the link address:

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.