A glimpse of how to build your Linux "Task Manager"

Source: Internet
Author: User
Tags linux task manager
A glimpse of how to build your Linux Task Manager in Linux, you must have used the Task Manager in Windows. in particular, we want to see how much CPU usage and memory are left, or when an exception occurs in a program and the program cannot be closed, you will definitely use the Task Manager to force the end... A glimpse of how to build your Linux "Task Manager"

In Windows, you must have used the Task Manager. in particular, we want to see how much CPU usage, memory is left, or when a program fails to be shut down due to an exception, you will certainly use the Task Manager to forcibly end this program. Is there such a tool in Linux? The answer is yes. as a powerful operating system, how can Linux not have a "task manager? We can use a series of tools and commands to achieve everything we do in the Windows task management, build a Linux "Task Manager ". Although it is not as intuitive as the Job Manager in Windows, it is very powerful. Next I will use the job management in Windows as a comparison to introduce the use of these tools.

1. View applications
In the task management program in Windows, we can easily and intuitively see the application we opened, and how to view us in Linux (that is, the current user) what about the opened application? First, we will introduce the simplest command-ps, which can select the running status of processes at a certain time point. We can do this:

First, I opened a browser program firefox (some open error information is in the middle, and the reader can ignore it temporarily), and then I used the command ps-l to view my bash opened Program, we can see from the above that the root user opens the program bash, firefox and ps. Why is there a ps program? Because you just entered the ps command to start the ps program to view the process, it is also a process opened by the root.

Next I will explain the meaning of each column in the output, because many subsequent commands will show similar information.
F: indicates the process permission. Generally, the value 4 or indicates that the process permission is root, and the value 1 indicates that the process can only be copied (fork) unable to actually execute (exec ).

S: the state of the process. The main states are R (Running), S (Sleep), D (Sleep state that cannot be awakened), T (stopped State ), Z ("zombie" status ).

UID: ID of the process owner (user). The root ID is 0;

PID: ID of the process

PPID: the id of the parent process of this process. we can see that the parent processes of ps and firefox are both bash. of course, they are all generated by the bash process.

C: CPU usage. unit: percentage;

PRI: Priority, indicating the Priority of the process executed by the CPU. the smaller the value, the faster the process is executed by the CPU.

NI: Nice, also indicates the priority of the process being executed by the CPU. The difference is that PRI is specified by the system, while NI can be specified by the user, and PRI (new) = PRI (old) + NI. to put it simply, PRI cannot be determined by the user, but the user can schedule the execution priority of the process through NI, root sets the range of NI to-20 ~ 19. for normal users, 0 ~ 19 (avoid general users from occupying system resources)

ADDR/SZ/WCHAN: both are related to memory. ADDR indicates the location of the process in the memory, running is "-", SZ indicates the memory usage, and WCHAN indicates whether the process is running, "-" also indicates that it is running.

TTY: terminal location; TIME: cpu time used out, note that it is not the system TIME; CMD: Command for generating this program.

See? This is more detailed than what you see in the Windows Task Manager.

2. View processes
In the Windows Task Manager, we can easily view which processes are running in the system, which users correspond to these processes, and the CPU and memory usage. Is there any good tool in Linux that can implement this function? Here is another tool-top. Compared with ps, it is static and can only select the process status at one time point. The top is to dynamically continuously check the running status of the process.

1. Use ps for static viewing
Let's start with the ps tool. In fact, ps can also implement this function. the method is as follows:

Note: ps aux does not have "-".

Because there are a lot of processes in Linux, they are not displayed here. the meaning of parameters in each column is the same as that in the first point, two more columns than in the first point: VSZ indicates the virtual memory used by the process (KB), and RSS indicates the fixed memory occupied by the process (KB ).

If you only look at the situation of a process, what should you do? It is very simple. for example, if I want to see the situation of the firefox process, the operation is as follows:

By using the command pipeline, you can filter the ps Output Content. comparing the output at the first point, we can see that PID 2576 is indeed the firefox process ID, which is very convenient and simple.

2. use top for dynamic viewing
Well, you may say that the process viewing in the Windows Task Manager is a dynamic detection. Don't worry. next we will introduce a more powerful and useful tool-top. Next I will introduce the most simple top usage, and there are many features of top. you can refer to man here. The procedure is as follows:

The top-d number indicates the number of updates every several seconds. the preceding command indicates that the detection is updated every five seconds.

You can divide the output result into two parts based on the black crude line. The last part is system information, usage of system resources, and number of processes. The next part is the same as previously mentioned, is the process in the system, and its meaning is the same as described above.

Here we will talk about the significance of the first part of the content above the black line.

Line 1: the current time is 10:58:59, the time it takes to start up is, the number of users logged on to the system is 2, the average load of the system in 1, 5, 15 minutes, it indicates that the system is responsible for running several processes on average in 1, 5, and 15 minutes. The smaller the system is, the more idle the system is. if it is higher than 1, the system is busy.

Row 2: displays the total number of processes and the status of individual processes. There are a total of 135 processes, 1 running, 134 sleeping, 0 stopping, and 0 global processes. The last one is the number of "zombie" processes. if it is not 0, check to see which process has become a zombie.

Row 3: overall CPU load

Line 4 and Line 5: physical memory and virtual memory usage.

Note: most of the time, we will see that the system does not run any program, but the memory is almost used up. please do not worry, the memory usage is normal, because the Linux system to speed up the system performance, therefore, the recently used file data will be cached. in this way, when the system wants to use the file in the future, the file will be directly retrieved from the memory instead of being transferred to the memory from the hard disk. Note the usage of swap (virtual memory). generally, it is best not to use swap.

3. view the process tree or parent process
In the previous example, we can see that many processes have a parent process. In fact, all processes in Linux are init sub-processes. how can we view the parent process of a process? This is not possible in the job manager in Windows.

1) use pstree to view details. the operations are as follows:

Note: A: The ASCII code is used to display the connection. p lists the PID. it can be seen from the above that all processes are init sub-processes.

2) view with ps
Yes, you can still use the ps tool for viewing. how is it? The operation is as follows:

Some parts in the middle are omitted ......


We can see that it lists the relationship between processes we mentioned in the first point, that is, bash is the parent process of firefox and ps.

3. end the task and process
In Windows, we often use the task manager to end a process that has encountered an exception and fails to close the program or end a process. This may also be the purpose of most Windows users to use the task manager. What should I do in Linux? We also have useful tools. here we will share two common tools: kill and killall.
1) use kill
In fact, both kill and killall need to send a signal to the process to tell how you want to end the process. There are three commonly used information, each of which has a code and name. The above code and name are equivalent, as shown below:
1 (SIGHUP) to start the terminated process, which is equivalent to restarting.
9 (SIGKILL), which forces the execution of a process to be interrupted.
15 (SIGTERM), terminate the process with a normal termination process. Note that if a problem occurs in the process, it is useless to enter this signal when the normal method is unavailable.

Next let's take a look at the use of kill, the operation is as follows:

First, use ps-l to find the ID of the process to be deleted, and then use kill to delete the process. you can see that firefox has been finally viewed later. The usage of kill is kill-signal PID. Because it is followed by a PID, it is often used together with ps, pstree and other commands.

2) use killall
I know you want to say that kill is very troublesome. I want to terminate a process and find its PID. it is really not refreshing, is there a way for me to signal based on the name of the command being executed? The answer is yes. it is killall. The procedure is as follows:

Here we re-run the firefox process, and then make killall come to the end of the process. from the final output, we can see that firefox has been properly implemented. How is it? Very convenient! In addition, killall is used as the killall-signal Command name.

Note: If you want to force the process to end as in windows, change signal to the name SIGKILL corresponding to 9 or 9, which applies to kill and killall.

IV. View network conditions
In the Windows Task Manager, there is also an information page called "networking", although you may not pay much attention to it in Windows (at least I do ), however, network usage is very important in Linux, because Linux is usually used as a server system. But is there any tool like this in Linux? Of course, netstat is used, as shown in the following figure:


As I am not very familiar with the network, I will not explain it much here. you can use man netstat for a moment.

V. Other tools
Although some of the previous tools (such as top) can view various resources of the system, these tools may generate more information you don't want to care about, for example, I just want to check the CPU and memory usage. if I use top, it will generate a lot of process information that I don't care about. So the following describes some specific functional tools.

1. free: View memory usage
Free is very easy to use. The operation is as follows:


In the command, m indicates that the unit of output data is MB. of course, you can also use B, k, g, and so on. You can also add a parameter t to display the total amount of physical memory and swap.

2. uptime: check the system startup time and workload. you can simply enter uptime directly without further details.

3. vmstat: detects system resource changes
Vmstat can realize the CPU, memory, and disk input and output statuses. The following example shows how to use this tool.
1) Count the current host CPU status, 1 time per second, 2 times in total, the operation is as follows:

Let me explain the meanings of some columns.
R: number of processes waiting for running. B: number of processes that cannot be awakened. the larger the mathematics of the two options, the busier the system is.
Si: the amount of data retrieved by the program in the disk. so: the capacity of the swap to which the unused program is written to the disk due to insufficient memory.
Bi: number of blocks written by the disk, and bo: number of blocks written to the disk. This value is high, indicating that the system I/O is busy.
In: Number of interrupted processes per second, cs: number of event switches per second;
Us: Non-kernel layer CPU usage status; sy kernel layer CPU usage status, id: idle status; wa: CPU consumption waiting for I/O; st: the CPU usage status stolen by the virtual machine.

2) Check the read/write status of all disks on the system.
The operation is very simple. just enter vmstat-d.

VI. Summary
Now you can see that the Linux "Task Manager" is more powerful than the Windows Task Manager. It mainly consists of ps, pstree, top, kill, killall, free, netstat, uptime, vmstat and other tools.

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.