Common Shell skills system running status

Source: Internet
Author: User

Shell commands related to the system running status:

1. Linux real-time monitoring command (watch ):
Watch is a very practical command that can help you monitor the running results of a command in real time, saving you the need to manually run it all over again. The two most common options for this command are-d and-n.-n indicates the number of seconds at which a "command" is executed, and-d indicates the position where the brightness changes. The following lists several common real-time monitoring commands in watch:
/> Watch-d-n 1 'who'# Execute the who command every second to monitor the current logon status of the server.
Every 1.0 s: who Sat Nov 12 12:37:18 2011

Stephen tty1 2011-11-11 :38 (: 0)
Stephen pts/0 2011-11-11 17: 39 (: 0.0)
Root pts/1 2011-11-12 :01 (192.168.149.1)
Root pts/2 2011-11-12 :41 (192.168.149.1)
Root pts/3 2011-11-12 12:11 (192.168.149.1)
Stephen pts/4 2011-11-12 12:22 (: 0.0)
Log on to the current Linux server as the root using other Linux client tools, and then observe the running changes of the watch command.
Every 1.0 s: who Sat Nov 12 12:41:09 2011

Stephen tty1 2011-11-11 :38 (: 0)
Stephen pts/0 2011-11-11 17: 39 (: 0.0)
Root pts/1 2011-11-12 :01 (192.168.149.1)
Root pts/2 2011-11-12 :41 (192.168.149.1)
Root pts/3 2011-11-12 12:40 (192.168.149.1)
Stephen pts/4 2011-11-12 12:22 (: 0.0)
Root pts/5 2011-11-12 12:41 (192.168.149.1)
The highlighted user in the last line is the new logged-on root user. Press CTRL + C to exit the ongoing watch monitoring process.

# Watch can run multiple commands at the same time, separated by semicolons.
# The following command monitors the disk usage and file changes in the current directory, including file addition, deletion, and file modification date updates.
/> Watch-d-n 1 'df-h; ls-l'
Every 1.0 s: df-h; ls-l Sat Nov 12 12:55:00 2011

Filesystem Size Used Avail Use % Mounted on
/Dev/sda1 5.8 GB 3.3G 2.2G 61%/
Tmpfs 504 M 420 K 504 M 1%/dev/shm
Total 20
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 root 183 Nov 11 08:02 users
-Rw-r --. 1 root 279 Nov 11 08:45 users2
In another Linux Console window, run the following command in the directory monitored by watch, such as/home/stephen/test.
/> Touch aa# After the command is executed, the other console that executes the watch command will change as follows:
Every 1.0 s: df-h; ls-l Sat Nov 12 12:57:08 2011

Filesystem Size Used Avail Use % Mounted on
/Dev/sda1 5.8 GB 3.3G 2.2G 61%/
Tmpfs 504 M 420 K 504 M 1%/dev/shm
Total 20
-Rw-r --. 1 root 0 Nov 12 12:56 aa
-Rw-r --. 1 root 0 Nov 12 10:02 datafile3
-Rw-r --. 1 root 10530 Nov 11 23:08 test.tar.bz2
-Rw-r --. 1 root 183 Nov 11 08:02 users
-Rw-r --. 1 root 279 Nov 11 08:45 users2
The highlighted yellow part is the highlighted part of the watch output after the touch aa command is executed.


2. view the current system memory usage (free ):
The free command has the following common options:

Option description-B displays data in bytes. -K displays data in kilobytes (KB) (default value ). -M displays data in MB. -S delay this option will enable free to refresh continuously. The interval between each refresh is the number of seconds specified by delay. If it contains a decimal point, it will be precise to milliseconds, for example, 0.5 is 500 milliseconds, 1 is a second.

The free command output table contains the following columns:

The column name indicates the total physical memory size. The amount of memory used by used. Free memory available. Total memory Shared by multiple processes. The cache size of the Buffers/cached disk.


See the following examples and output instructions:
/> Free-k
Total used free shared buffers cached
Mem: 1031320 671776 359544 0 88796 352564
-/+ Buffers/cache: 230416 800904
Swap: 204792 0 204792
For the output of the free command, we only need to pay attention to the output lines highlighted in red and the output lines highlighted in green. For details, see the following:
Red output row: This row looks at the output data from the operating system perspective. used (671776) indicates Kernel + Applications + buffers + cached. Free (359544) indicates the amount of memory available in the system.
Green output line: This line outputs data from the application perspective. Its free = operating system used + buffers + cached, both:
800904 = 359544 + 88796 + 352564
/> Free-m
Total used free shared buffers cached
Mem: 1007 656 351 0 86 344
-/+ Buffers/cache: 225 782
Swap: 199 0 199
/> Free-k-s 1.5# Display data in kilobytes (KB) and refresh the output every 1.5 until you press CTRL + C to exit
Total used free shared buffers cached
Mem: 1007 655 351 0 86 344
-/+ Buffers/cache: 224 782
Swap: 199 0 199

Total used free shared buffers cached
Mem: 1007 655 351 0 86 344
-/+ Buffers/cache: 224 782
Swap: 199 0 199

3. Real-time CPU monitoring tool (mpstat ):
This command is used to report the real-time running status of all CPUs in the current system.
# This command outputs the current running status of the CPU every 2 seconds, and outputs 5 times in total. If there is no second numeric parameter, mpstat will be executed every 2 seconds, press CTRL + C to exit.
/> Mpstat 2 5
Linux 2.6.32-71. el6.i686 (Stephen-PC) 11/12/2011 _ i686 _ (1 CPU)

At 04:03:00 CPU % usr % nice % sys % iowait % irq % soft % steal % guest % idle
04:03:02 all 0.00 0.00 0.50 0.00 0.00 0.00 0.00 0.00 99.50
04:03:04 all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
04:03:06 all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
04:03:08 all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
04:03:10 all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
Average: all 0.00 0.00 0.10 0.00 0.00 0.00 0.00 0.00 99.90

The end of the first line shows the number of CPUs in the current system. The following table shows the current CPU usage of the system. The meanings of each column are as follows:

Description of the column name % user CPU time (%) in the user State during the internal period, excluding the nice value for negative processes (usr/total) * 100% nice during the internal period, nice value: CPU time of the negative process (%) (nice/total) * 100% sys Kernel Time (%) (system/total) in the internal Period) * 100% iowait hard disk IO wait time (%) (iowait/total) * 100% irq hard interrupt time (%) (irq/total) during internal) * 100% soft interrupt time (%) (softirq/total) * 100% idle during internal, CPU idle time (%) (idle/total) * 100 except waiting for disk I/O operations

Calculation formula:
Total_cur = user + system + nice + idle + iowait + irq + softirq
Total_pre = pre_user + pre_system + pre_nice + pre_idle + pre_iowait + pre_irq + pre_softirq
User = user_cur-user_pre
Total = total_cur-total_pre
_ Cur indicates the current value, and _ pre indicates the value before the interval time. All values in the preceding table can be two decimal places.

/> Mpstat-p all 2 3#-P all indicates printing all cpu data. Here, you can also print the CPU data of the specified number, for example,-P 0 (the CPU number starts with 0)
Linux 2.6.32-71. el6.i686 (Stephen-PC) 11/12/2011 _ i686 _ (1 CPU)

At 04:12:54 CPU % usr % nice % sys % iowait % irq % soft % steal % guest % idle
04:12:56 all 0.00 0.00 0.50 0.00 0.00 0.00 0.00 0.00 99.50
04:12:56 PM 0 0.00 0.00 0.50 0.00 0.00 0.00 0.00 0.00 99.50

At 04:12:56 CPU % usr % nice % sys % iowait % irq % soft % steal % guest % idle
04:12:58 all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
04:12:58 PM 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00

At 04:12:58 CPU % usr % nice % sys % iowait % irq % soft % steal % guest % idle
04:13:00 all 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00
04:13:00 PM 0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 100.00

Average: CPU % usr % nice % sys % iowait % irq % soft % steal % guest % idle
Average: all 0.00 0.00 0.17 0.00 0.00 0.00 0.00 0.00 99.83
Average: 0 0.00 0.00 0.17 0.00 0.00 0.00 0.00 0.00 99.83

4. Virtual Memory real-time monitoring tool (vmstat ):
The vmstat command is used to obtain information about processes, virtual memory, page swap space, and CPU activity in UNIX systems. This information reflects the system load. When vmstat is run for the first time, statistics from the start of the system are displayed. When vmstat is run, statistics from the last run of the command are displayed. You can specify the number and time of statistics to obtain the required statistics.
/> Vmstat 1 3# This is the most common method of vmstat. It means to output one message every one second. After three messages are output, the program exits.
Procs ----------- memory ---------- --- swap -- ----- io ---- system -- ----- cpu -----
R B swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 531760 67284 231212 0 0 108 260 111 1 5 86 8 0
0 0 0 531752 67284 231212 0 0 0 33 57 0 1 99 0 0
0 0 0 531752 67284 0 0 0 0 40 73 0 0 231212 0 0

/> Vmstat 1# It means to output one entry every 1 second until you press CTRL + C to exit.

The following describes the meaning of each column in the output table:
The process information is as follows: (procs)
R: Number of processes waiting in the ready state.
B: Number of waiting processes.
Memory information: (memory)
Swpd: the size of the swap in use, in KB.
Free: idle memory space.
Buff: the size of the used buff to buffer the read/write operations of Block devices.
Cache: used cache size, file system cache.
Information about the page swap space is: (swap)
Si: swap memory usage, transferred from disk to memory.
So: swap memory usage, transferred from memory to disk.
Information about IO Block devices is as follows: (io)
Bi: total data read from Block devices (read disk) (KB/s)
Bo: data written to block devices (written to disk) (KB/s)
Fault Information: (system)
In: The number of interruptions per second within the specified time.
Sy: Number of system calls per second within the specified time.
Cs: the number of context switches per second within the specified time.
CPU information: (cpu)
Us: CPU usage in user mode within a specified time interval.
Sy: CPU usage in the core State within a specified interval.
Id: CPU idle time ratio within the specified time interval.
Wa: the ratio of idle time when the CPU waits for I/O within the specified interval.
Vmstat can be used to determine whether a system is limited by CPU or memory: if the percentage of CPU sy and us values is close to 100%, or the running Queue (r) the number of waiting processes is always not equal to 0, and often greater than 4, and the id is often less than 40, the system is limited by the CPU; if the value of bi and bo is always not equal to 0, the system is limited by memory.

5. Real-time Monitoring Tool (iostat) for device I/O load ):
Iostat is mainly used to monitor the IO load of system devices. iostat displays the statistics from the start of the system when it is run for the first time. After iostat is run, statistics from the last run of the command are displayed. You can specify the number and time of statistics to obtain the required statistics.
The most common usage of this command is as follows:
/> Iostat-d 1 3# Only display the IO load of the device. Refresh and output the result once every 1 second. Output three times and exit iostat.
Linux 2.6.32-71. el6.i686 (Stephen-PC) 11/16/2011 _ i686 _ (1 CPU)

Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
Sda 5.35 258.39 26.19 538210 54560

Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
Sda 0.00 0.00 0.00 0 0

Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
Sda 0.00 0.00 0.00 0 0

Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
Sda 0.00 0.00 0.00 0 0
/> Iostat-d 1# Like the preceding command, the command is refreshed and output every one second, but the command will be output until you press CTRL + C to exit.
The meanings of each column in the output table are as follows:

The column name indicates the number of blocks (slice) read per second by Blk_read/s. The number of block (sector) writes per second in Blk_wrtn/s. Total number of Blk_read blocks (slice. Total number of Blk_wrtn block (sector) writes.

Iostat also has a common option-x, which is used to display io-related extended data.
/> Iostat-dx 1 3
Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm % util
Sda 5.27 1.31 2.82 1.14 189.49 19.50 52.75 0.53 133.04 10.74 4.26

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm % util
Sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00

Device: rrqm/s wrqm/s r/s w/s rsec/s wsec/s avgrq-sz avgqu-sz await svctm % util
Sda 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
You can also specify the device name to be monitored in the command line parameters, such:
/> Iostat-dx sda 1 3# Specify the monitoring device name as sda. The output result of this command is exactly the same as that of the preceding command.

The meanings of each column in the table output by the extension options are as follows:

Column Name Description: Number of read requests merged per second in the rrqm/s queue wrqm/s queue number of write requests merged per second r/s number of read requests completed per second w/s completion per second number of write requests rsec/s number of sectors read per second wsec/s number of sectors written per second avgrq-sz average request sector size avgqu-sz average request queue length await average the wait time of each request. Utilization of the util Device

The following is an explanation of the key columns:
Util is the utilization rate of the device. If it is close to 100%, it usually indicates that the device capability tends to be saturated.
Await is the average wait time for each request. This time includes the queue time and service time. That is to say, in general, await is larger than svctm, and the smaller the difference, the shorter the queue time, and the larger the difference, the longer the queue time, it indicates a problem with the system.
Avgqu-sz is the length of the average request queue. Undoubtedly, the shorter the queue length, the better.

6. Real-time Monitoring Tool (pidstat) for the current running process ):
Pidstat is mainly used to monitor system resources occupied by all or specified processes, such as CPU, memory, device IO, task switching, and threads. When pidstat is run for the first time, statistics from the start of the system are displayed. When pidstat is run, statistics from the last run of the command are displayed. You can specify the number and time of statistics to obtain the required statistics.
In normal use, the pid to be monitored is usually specified in the command line option, and then the system resource information related to the pid is monitored through other specific parameters.

Option description-l displays information about the process and CPU (the complete command path name and command parameters can be displayed in the command column ). -D: displays the process and device IO information. -R displays information about the process and memory. -W: displays information about the process and task time slice switchover. -T displays information about the running threads in the process. -P is followed by a monitored process id or ALL (indicating ALL processes). If this option is not specified, ALL processes running on the current system are monitored.

# Monitor the CPU resource usage of processes whose pid is 1 (init). Refresh and output every two seconds. After three times, the program exits.
/> Pidstat-p 1 2 3-l
07:18:58 am pid % usr % system % guest % CPU Command
07:18:59 AM 1 0.00 0.00 0.00 0.00 0/sbin/init
07:19:00 AM 1 0.00 0.00 0.00 0.00 0/sbin/init
07:19:01 AM 1 0.00 0.00 0.00 0.00 0/sbin/init
Average: 1 0.00 0.00 0.00-/sbin/init
% Usr: CPU usage of the Process in user mode.
% System: CPU usage of the Process in kernel mode (system level.
% CPU: The total CPU usage of the process. If it is in an SMP environment, the value is divided by the number of CPUs to show the data of each CPU.
CPU: the number of the CPU attached to the process (0 indicates the first CPU ).

# Monitor the device IO resource load of processes whose pid is 1 (init). Refresh and output every two seconds. After three times, the program exits.
/> Pidstat-p 1 2 3-d
07:24:49 am pid kB_rd/s kB_wr/s kB_ccwr/s Command
07:24:51 AM 1 0.00 0.00 0.00 init
07:24:53 AM 1 0.00 0.00 0.00 init
07:24:55 AM 1 0.00 0.00 0.00 init
Average: 1 0.00 0.00 0.00 init
KB_rd/s: the number of bytes read by the process per second (KB ).
KB_wr/s: the number of bytes written by the process per second (KB ).
KB_ccwr/s: Number of disk writes canceled by this process per second (KB ).

# Monitor the memory usage of processes whose pid is 1 (init). Refresh and output every two seconds. After three times, the program exits.
/> Pidstat-p 1 2 3-r
07:29:56 am pid minflt/s majflt/s vsz rss % MEM Command
07:29:58 AM 1 0.00 0.00 2828 1368 init
07:30:00 AM 1 0.00 0.00 2828 1368 init
07:30:02 AM 1 0.00 0.00 2828 1368 init
Average: 1 0.00 0.00 2828 1368 0.13 init
% MEM: memory usage percentage of the process.

# Monitor the process task switching with a pid of 1 (init). Refresh and output every two seconds. After three times, the program exits.
/> Pidstat-p 1 2 3-w
07:32:15 am pid cswch/s nvcswch/s Command
07:32:17 AM 1 0.00 0.00 init
07:32:19 AM 1 0.00 0.00 init
07:32:21 AM 1 0.00 0.00 init
Average: 1 0.00 0.00 init
Cswch/s: the number of active (voluntary) context switches per second. Active switching means that when a task is in a blocking wait state, it will take the initiative to give up its CPU resources.
Nvcswch/s: the number of times the task is passive (not voluntary) to switch the context per second. Passive switching means that the time slice allocated by the CPU to a task has been used up, so the process will be forced to give up the CPU execution right.

# Monitor the memory usage of processes whose pid is 1 (init) and their internal threads (r option). Refresh and output every two seconds. After three times, the program exits. Note that if the-t option is not followed by any other options, CPU resources are monitored by default. The yellow highlighted part of the result indicates that the process and its internal threads are displayed in a tree structure.
/> Pidstat-p 1 2 3-tr
Linux 2.6.32-71. el6.i686 (Stephen-PC) 11/16/2011 _ i686 _ (1 CPU)

07:37:04 am tgid tid minflt/s majflt/s vsz rss % MEM Command
07:37:06 AM 1-0.00 0.00 2828 1368 0.13 init
07:37:06 AM-1 0.00 0.00 2828 1368 0.13 |__ init

07:37:06 am tgid tid minflt/s majflt/s vsz rss % MEM Command
07:37:08 AM 1-0.00 0.00 2828 1368 0.13 init
07:37:08 AM-1 0.00 0.00 2828 1368 0.13 |__ init

07:37:08 am tgid tid minflt/s majflt/s vsz rss % MEM Command
07:37:10 AM 1-0.00 0.00 2828 1368 0.13 init
07:37:10 AM-1 0.00 0.00 2828 1368 0.13 |__ init

Average: tgid tid minflt/s majflt/s vsz rss % MEM Command
Average: 1-0.00 0.00 2828 1368 0.13 init
Average:-1 0.00 0.00 2828 1368 0.13 |__ init
TGID: ID of the thread group.
TID: thread ID.

The options for monitoring different resources can exist at the same time, so that multiple resource usage will be output at a time, such as pidstat-p 1-dr.

7. Report disk space usage (df ):
The most common option of this command is-h, which intelligently outputs data units to make output results more readable.
/> Df-h
Filesystem Size Used Avail Use % Mounted on
/Dev/sda1 5.8 GB 3.3G 2.2G 61%/
Tmpfs 504 M 260 K 504 M 1%/dev/shm

8. Evaluate the disk usage (du ):

Option description-a includes all files, not just directories. -B is measured in bytes. -K is measured in kilobytes (KB. -M is measured in MB. -H makes the output information easier to read. -S only displays the total space occupied by the working directory. -- Exclude = PATTERN: exclude files that conform to the style. Pattern is a normal Shell style ,? Represents any character. * represents any number of characters. -- Max-depth = N from the current directory, the subdirectories with a directory depth greater than N will not be calculated. This option cannot coexist with the s option.

# Only displays information about sub-level directories.
/> Du -- max-depth = 1-h
246 M./stephen
246 M.
/> Du-sh ./*# Obtain the disk space occupied by all subdirectories in the current directory.
352 K./MemcachedTest
132 K./Test
33 M./thirdparty
# In the current directory, exclude the sub-directories (./Test) whose directory name mode is Te * and output the disk space occupied by other sub-directories.
/> Du -- exclude = Te *-sh ./*
352 K./MemcachedTest
33 M./thirdparty

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.