Linux--Viewing of processes, acquisition of process IDs, killing of processes

Source: Internet
Author: User

Process View

PS Ax: Displays a list of current system processes

PS aux: Displays a detailed list of current system processes and process users

PS ax|less: If the output is too long, you may add a pipeline command less to view the specific process, such as: PS ax|grep xxx (XXX for process name)

Get process ID

How the shell obtains the process ID: ps-a |grep "CmdName" | awk ' {print $ ' pidof ' cmdname ' pgrep ' cmdname ' The three kinds of running results in bash and BusyBox ash are slightly different, the first is exactly the same, but because of the number of calls to the command, performance is not possible. Second: Pidof can only get the process number to which the program's file name matches, and in ash, such as Pidof "usr/bin/telnetd" and Pidof "telnetd", the result is not the same, but the previous result is empty, but executes both in bash. The third: Pgrep is the same as 1, because it is a single command that is much better than the first performance. So NANORC was improved to pgrep. process kills

1. Role
The KILL command is used to abort a process.

2. Format
Kill [-S signal |-P] [-a] pid ...
Kill-l [Signal]

3. Parameters
-S: Specifies the signal to send.
-P: Analog send signal.
-L: Specifies a list of names for the signal.
PID: The ID number of the process to abort.
Signal: Indicates a signal.

4. Description
Processes are a very important concept in a Linux system. Linux is a multitasking operating system that often runs multiple processes at the same time. We don't care how these processes are distributed or how the kernel manages the allocation of time slices, and the concern is how to control these processes so that they can serve the user well.

The Linux operating system consists of three different types of processes, each of which has its own characteristics and attributes. An interactive process is a process that is initiated by a shell. The interactive process can be run either in the foreground or in the background. The batch process and terminal are not linked, and are a sequence of processes. A process that is started when the Linux system starts and runs in the background when the process is monitored (also called the System daemon). For example, HTTPD is the well-known Apache server monitoring process.

The KILL command works by sending a system operation signal to the kernel of the Linux system and the process identification number of a program, and then the system kernel can operate on the processes specified by the process identification number. For example, in the top command, we see that the system runs many processes, and sometimes it needs to use kill to abort some processes to improve system resources. When explaining the installation and landing commands, it was mentioned that the function of multiple virtual consoles is to switch to another virtual Console to shut down the program when a program error causes the system to deadlock. The command that is used at this point is kill, because Kill is a direct call to most shell internal commands.

5. Application Examples
(1) Forcibly abort (often used to kill) a process with an identification number of 324:
#kill-9 324

(2) Unlocking the Linux system deadlock
In Linux, there are times when a program crashes and is in a deadlock state. You do not have to restart your computer at this time, just abort (or shut down) the problematic program. When Kill is in the X-window interface, the main program (except for the crashing program) normally starts. At this point, open a terminal, where the problematic program is aborted. For example, if the Mozilla browser program has a deadlock situation, you can use the KILL command to abort all programs that contain the Mozolla browser. First use the top command to investigate the PID of the program, and then use the KILL command to stop the program:
#kill-sigkill XXX
where xxx is the process identification number of the program that contains the Mozolla browser.

(3) Use the command to reclaim memory
We know that memory is very important to the system, and reclaiming memory can improve system resources. The KILL command can be used in a timely manner to abort some "deviant" programs or for a long time without corresponding programs. For example, using the top command to discover a useless (Zombie) process, you can use the following command:
#kill-9 XXX
where xxx is a useless process identification number.

Then use the following command:
#free
You will see an increase in available memory capacity.

(4) Killall command

Linux also provides a killall command that can directly use the name of the process rather than the process identification number, for example:

# killall-hup inetd

* The safest way to kill a process is to simply use the KILL command, without modifiers, without flags.

First use the PS-EF command to determine the PID to kill the process, and then enter the following command:

# Kill-pid

Note: A standard KILL command usually achieves its purpose. Terminate the problematic process and release the resources of the process to the system. However, if a process starts a child process and kills only the parent process, the child process is still running and therefore consumes resources. To prevent these so-called "zombie processes," Be sure to kill all of their child processes before killing the parent process.


--------------------------------------------------------------------------------

* Determine the PID or ppid to kill the process

# Ps-ef | grep httpd
--------------------------------------------------------------------------------


* End the process in an elegant way

# kill-l PID

The-l option tells the kill command to end the process in a way that appears to the user who started the process logged off. When this option is used, the kill command also attempts to kill the left child process. But this command is not always successful--you may still need to manually kill the child process before killing the parent process.
--------------------------------------------------------------------------------


*term Signal

Sends a term signal to the parent process, attempting to kill it and its child processes.

# kill-term PPID
--------------------------------------------------------------------------------


*killall command

The killall command kills all processes within the same process group. It allows you to specify the name of the process to terminate, not the PID.

# Killall HTTPD
--------------------------------------------------------------------------------


* Stop and restart process

Sometimes you just want to simply stop and restart the process. As follows:

# Kill-hup PID

This command shuts down the Linux gentle execution process and then restarts immediately. This command is handy when configuring the application, which can be executed when the configuration file is modified to restart the process.
--------------------------------------------------------------------------------


* Lore Kill-9 PID

Consent of the Kill-s SIGKILL

This powerful and dangerous command forces the process to terminate abruptly at run time, and the process cannot clean itself up after the end. A hazard is a system resource that is not normally released, and is generally not recommended unless other methods are invalid.

When using this command, be sure to confirm with ps-ef that there are no zombie processes left. The zombie process can be eliminated only by terminating the parent process. If the zombie process is adopted by INIT, the problem is more serious. Killing the init process means shutting down the system.

If there is a zombie process in the system, and its parent process is init, and the zombie process consumes a lot of system resources, then you need to restart the machine at some point to clear the process table.

Linux--Viewing of processes, acquisition of process IDs, killing of processes

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.