The N method of viewing the City (PS) and killing process (kill) under Linux

Source: Internet
Author: User
Tags pkill

PS Viewing process

The Inux process has 5 states:
1. Running (running or waiting in the running queue)
2. Interrupt (dormant, blocked, waiting for a condition to form or receive a signal)
3. Non-interruptible (receive signal does not wake up and not run, process must wait until interrupt occurs)
4. Zombie (The process has been terminated, but the process descriptor exists until the parent process calls WAIT4 () after the system call is released)
5. Stop (process received Sigstop, SIGSTP, Sigtin, Sigtou signal after stop running run)

PS Tool identifies 5 status codes for the process:
D non-interruptible uninterruptible sleep (usually IO)
R run runnable (on run queue)
S Interrupt Sleeping
T stop traced or stopped
Z Zombie a defunct ("zombie") process

Name: PS
Usage rights: All users
How to use: PS [options] [--help]
Description: Shows the dynamics of the instantaneous stroke (process)
Parameters:
The parameters of PS are very numerous, and only a few commonly used parameters are listed here and the meaning is roughly
-A lists all trips
-W display widen to show more information
-au Show more detailed information
-aux Show all itineraries that contain other users

AU (x) output format:

USER PID%cpu%MEM VSZ RSS TTY STAT START time COMMAND
User: Owner of the itinerary
Pid:pid
%CPU: CPU Usage consumed
%MEM: Usage of memory consumed
VSZ: Virtual Memory size occupied
RSS: Size of memory consumed
TTY: Secondary device number for the terminal (minor device numbers of TTY)
STAT: The status of the trip:
D: Non-interruptible static
R: Executing in progress
S: Stationary state
T: Pause execution
Z: Not present but cannot be eliminated temporarily
W: Not enough memory paging to allocate
<: High-priority stroke
N: Low-priority stroke
L: Memory paged and locked in memory (instant system or I/O)
Start: Trip start time
Time: When it was executed
Command: the command executed

Example:

Ps
PID TTY Time CMD
2791 ttyp0 00:00:00 tcsh
3092 Ttyp0 00:00:00 PS
% ps-a
PID TTY Time CMD
1? 00:00:03 Init
2? 00:00:00 kflushd
3? 00:00:00 Kpiod
6} 00:00:00 KSWAPD
5? 00:00:00 Mdrecoveryd
.......
% Ps-aux
USER PID%cpu%MEM VSZ RSS TTY STAT START time COMMAND
Root 1 0.0 0.7 1096 472? S Sep10 0:03 init [3]
Root 2 0.0 0.0 0 0? SW SEP10 0:00 [kflushd]
Root 3 0.0 0.0 0 0? SW SEP10 0:00 [Kpiod]
Root 4 0.0 0.0 0 0? SW SEP10 0:00 [KSWAPD]

The specific commands are explained as follows:
1) PS A shows all the programs under the current terminal, including other users ' programs.

2) ps-a Show All Programs.

3) PS C lists the program, displays the actual instruction name of each program, and does not include the path, parameter or the indication of the resident service.

4) Ps-e The effect of this parameter is the same as specifying the "A" parameter.

5) When listing the program, PS e displays the environment variables used by each program.

6) PS F Displays the tree structure with ASCII characters, expressing the relationship between the programs.

7) ps-h shows the tree structure, indicating the relationship between the programs.

8) Ps-n shows all the programs except the program under the PS Command Terminal.

9) PS s uses the program signal format to display the program status.

PS S when listing programs, including interrupted sub-program data.

One) ps-t< terminal number >

Specify the terminal number and list the status of the program that belongs to the terminal.

PS u

Displays the status of the program in a user-oriented format.

PS X

Show All programs, do not differentiate by terminal.

The most common approach is to Ps-aux, and then use a pipe symbol to direct to grep to find a specific process and then manipulate the specific process.

from:http://blog.csdn.net/cjh6311882/article/details/7611967

A detailed description of the grep command under Linux

1. Role
The grep command in a Linux system is a powerful text search tool that uses regular expressions to search for text and print matching lines. The grep full name is the global Regular Expr ession Print, which represents the universal regular expression version, which uses permissions for all users.

2. Format
grep [Options]

3. Main parameters
[Options] Main parameters:
-C: Outputs only the count of matching rows.
-I: Case insensitive (only for single-character).
-H: The file name is not displayed when querying multiple files.
-L: Only file names that contain matching characters are output when querying multiple files.
-N: Displays matching lines and line numbers.
-S: does not display error messages that do not exist or have no matching text.
-V: Displays all lines that do not contain matching text.
Pattern Regular Expression Main parameters:
\: Ignores the original meaning of special characters in regular expressions.
^: matches the start line of the regular expression.
$: Matches the end line of the regular expression.
\<: Starts from the line that matches the regular expression.
\>: End of line to match regular expression.
[]: A single character, such as [a], a meets the requirements.
[-]: range, such as [A-z], i.e. A, B, C to Z all meet the requirements.
。 : all the individual characters.
*: There are characters, the length can be 0.

4.grep command uses a simple instance
$ grep ' test ' d*
Displays all rows that contain test in a file that begins with D.
$ grep ' test ' AA bb cc
Displays the line that matches test in the aa,bb,cc file.
$ grep ' [a-z]\{5\} ' AA
Displays all rows that contain a string of at least 5 consecutive lowercase characters for each string.
$ grep ' w\ (es\) T.*\1′aa
If West is matched, es is stored in memory, labeled 1, and then searched for any character (. *) followed by another ES (\1), which is found to display the row. If you use Egrep or GREP-E, you do not have "\" number to escape, directly written as ' W (es) t.*\1′ on it.

5.grep commands use complex instances
Suppose you are searching for a file with the string ' magic ' in the '/usr/src/linux/doc ' directory:
$ grep magic/usr/src/linux/doc/*
sysrq.txt:* How do I enable the Magic SysRq key?
sysrq.txt:* How does I use the Magic sysrq key?
Where the file ' Sysrp.txt ' contains the string and discusses the functionality of SYSRQ.
By default, ' grep ' searches only the current directory. If there are many subdirectories under this directory, ' grep ' is listed in the following form:
Grep:sound:Is a Directory
This may make the output of ' grep ' difficult to read. There are two ways to solve this problem:
Explicit Requirements Search subdirectories: grep-r
or Ignore subdirectories: grep-d Skip
If you have a lot of output, you can go through the pipe to read it on ' less ':
$ grep magic/usr/src/linux/documentation/* | Less
This makes it easier for you to read.

One thing to note is that you need to provide a way to filter the file (search for all files in *). If you forget, ' grep ' will wait until the program is interrupted. If you are experiencing such a situation, press <ctrl c>, and then try again.

Here are some interesting command-line arguments:
Grep-i pattern Files: Search by case-insensitive. The default case is case-sensitive,
Grep-l pattern Files: Lists only the matching file names,
Grep-l pattern Files: Lists mismatched file names,
Grep-w pattern files: matches only the entire word, not part of the string (such as matching ' magic ', not ' magical '),
Grep-c number pattern files: matching contexts display [number] lines, respectively,
grep pattern1 | PATTERN2 files: Displays rows that match pattern1 or pattern2.
grep pattern1 Files | grep pattern2: Displays rows that match both PATTERN1 and pattern2.

Grep-n pattern files to display line number information

Grep-c pattern files to find total rows

Here are some special symbols for searching:
\< and \> each mark the beginning and end of the word.
For example:
grep man * will match ' Batman ', ' manic ', ' man ', etc.
grep ' \<man ' matches ' manic ' and ' man ', but not ' Batman ',
grep ' \<man\> ' matches only ' man ', not ' Batman ' or ' manic ' and other strings.
' ^ ': refers to a matching string at the beginning of the line,
' $ ': refers to a matching string at the end of the line,

Grep Command Usage Daquan

1. Parameters:
-I: Ignore case
-C: Print the number of matching rows
-L: Find included matches from multiple files
-V: Find rows that do not contain matches
-N: Print row and row labels that contain matches

2, RE (regular expression)
\ ignore the original meaning of special characters in regular expressions
^ matches the start line of the regular expression
$ matches the end line of a regular expression
\< starting from the line that matches the regular expression
\> to the end of the line that matches the regular expression
[] A single character, such as [a] that a meets the requirements
[-] range, such as [A-z], i.e. a,b,c to Z all meet the requirements
. All the individual characters
* All characters, length can be 0

Kill kills the process

General article:

First, use PS to view the process as follows:

$ ps-ef

......
Smx 1822 1 0 11:38? 00:00:49 gnome-terminal
Smx 1823 1822 0 11:38? 00:00:00 Gnome-pty-helper
Smx 1824 1822 0 11:38 pts/0 00:00:02 bash
Smx 1827 1 4 11:38? 00:26:28/usr/lib/firefox-3.6.18/firefox-bin
Smx 1857 1822 0 11:38 pts/1 00:00:00 bash
Smx 1880 1619 0 11:38? 00:00:00 Update-notifier
......
Smx 11946 1824 0 21:41 pts/0 00:00:00 ps-ef

Or:

$ ps-aux

......

Smx 1822 0.1 0.8 58484 18152? Sl 11:38 0:49 gnome-terminal
Smx 1823 0.0 0.0 1988 712? S 11:38 0:00 Gnome-pty-helper
Smx 1824 0.0 0.1 6820 3776 pts/0 Ss 11:38 0:02 Bash
smx 1827 4.3 5.8 398196 119568? Sl 11:38 26:13/usr/lib/firefox-3.6.18/firefox-bin
Smx 1857 0.0 0.1 6688 3644 pts/1 Ss 11:38 0:00 Bash
Smx 1880 0.0 0.6 41536 12620? S 11:38 0:00 Update-notifier
......
Smx 11953 0.0 0.0 2716 1064 pts/0 r+ 21:42 0:00 ps-aux

At this point, if I want to kill Firefox, the process is entered in the terminal:

$ kill-s 9 1827

Where-s 9 has developed a signal to pass to the process is 9, that is, forcing, terminate the process as soon as possible. The various termination signals and their effects are shown in the appendix.

1827 is the above PS found in the Firefox PID.

Simple, but there is a problem, the process is not indifferent, the process is more, it will feel pain, whether it is ps-ef or ps-aux, each time in a large string of process information to find the process to kill, look at the eyes are spent.

Advanced article:

Improvement 1:

The query results from PS are piped to grep to find the process that contains the specific string. Pipe symbol "|" Used to separate two commands, the output from the left command of the pipe character is entered as the command to the right of the pipe.

$ PS-EF | grep Firefox
Smx 1827 1 4 11:38? 00:27:33/usr/lib/firefox-3.6.18/firefox-bin
Smx 12029 1824 0 21:54 pts/0 00:00:00 grep--color=auto Firefox

It's refreshing this time. Then there is

$kill-S 9 1827

Or too much typing?

Improved 2--using pgrep:

What do you think first when you see Pgrep? Yes, grep!. Pgrep p indicates that this command is a grep dedicated to process queries.

$ pgrep Firefox
1827

What did you see? Yes, the PID of Firefox, and then typing again:

$kill-S 9 1827

Improved 3--using pidof:

See what pidof think? Yes, pid of xx, literally translated is xx pid.

$ pidof Firefox-bin
1827
Slightly less significant than pgrep is that pidof must give the full name of the process. Then there is the cliché:

$kill-S 9 1827

Whether using PS and then slowly finding the process PID or using grep to find the process containing the corresponding string, or using pgrep directly to find the process PID containing the corresponding string, and then manually input to kill, is a bit cumbersome. Is there a more convenient way? Yes!

Improvement 4:

$ps-ef | grep Firefox | Grep-v grep | Cut-c 9-15 | Xargs Kill-s 9

Description

The result of "grep Firefox" is that all processes that contain the keyword "Firefox".

Grep-v grep removes the process that contains the keyword "grep" in the listed process.

"Cut-c 9-15" is the 9th character to the 15th character of the input line, which is exactly the process number PID.

The Xargs command in "Xargs kill-s 9" is used to take the output (PID) of the preceding command as a parameter to the "Kill-s 9" command and execute the command. "Kill-s 9" will forcibly kill the specified process.

Don't you want to complain about something? Yes, it's too long.

Improvement 5:

Know Pgrep and pidof two orders, why do you want to play so long a string!

$ pgrep Firefox | Xargs Kill-s 9

Improvement 6:

$ PS-EF | grep Firefox | awk ' {print $} ' | Xargs kill-9
Kill:no such process

There is a more depressed place, the process has been correctly found and terminated, but the execution is not prompted to find the process.

The role of awk ' {print $} ' is to print out the contents of the second column. According to the general article, you can know that the second column of PS output is exactly PID. The PID of the process corresponding to the Xargs passed to kill as parameters, kill the corresponding process.

Improvement 7:

Do I have to call Xargs to pass the PID to kill every time? The answer is in the negative:

$kill-S 9 ' Ps-aux | grep Firefox | awk ' {print $} '

Improvement 8:

Yes, the order is still a bit long and replaced by Pgrep.

$kill-S 9 ' pgrep Firefox '

Improved 9--pkill:

What did you see Pkill think of? Yes, Pgrep and kill!. Pkill=pgrep+kill.

$pkill-9 Firefox

Note: "9" is the signal sent is 9,pkill and kill in this difference is: Pkill no "s", the terminating signal level directly followed by "-". I always thought it was "-s 9", and the result was that each run could not terminate the process.

Improved 10--killall:

Killall and Pkill are similar, but if the given process name is incomplete, Killall will give an error. Pkill or pgrep can terminate a process as long as a part of the process name is given.

$killall-9 Firefox

Appendix: Various signals and their uses

Signal Description Signal number on Linux x86[1]
Sigabrt Process aborted 6
Sigalrm Signal raised by alarm 14
Sigbus Bus error: "Access to undefined portion of memory object" 7
SIGCHLD Child process terminated, stopped (or continued*) 17
Sigcont Continue if stopped 18
SIGFPE Floating point exception: "Erroneous arithmetic operation" 8
SIGHUP Hangup 1
Sigill Illegal instruction 4
SIGINT Interrupt 2
SIGKILL Kill (Terminate immediately) 9
Sigpipe Write to pipe with no one reading 13
Sigquit Quit and Dump Core 3
SIGSEGV Segmentation violation 11
SIGSTOP Stop Executing temporarily 19
SIGTERM Termination (Request to terminate) 15
Sigtstp Terminal Stop Signal 20
Sigttin Background process attempting to read from TTY ("in") 21st
Sigttou Background process attempting to write-to-TTY ("Out") 22
SIGUSR1 User-defined 1 10
SIGUSR2 User-defined 2 12
Sigpoll Pollable Event 29
Sigprof Profiling Timer expired 27
Sigsys Bad Syscall 31
SIGTRAP Trace/breakpoint Trap 5
Sigurg Urgent data available on socket 23
Sigvtalrm Signal raised by Timer counting virtual time: "Virtual timer expired" 26
Sigxcpu CPU time limit exceeded 24
Sigxfsz File size limit exceeded 25

The N method of viewing the City (PS) and killing process (kill) under Linux

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.