We deal with Linux servers every day, especially Linux engineers. To ensure the security and performance of the server, we often need to monitor the status of the server to ensure that the work can be carried out smoothly.

The commands described in this article apply not only to server monitoring, but also to development in our everyday situations.

1. watch

Watch command is frequently used. Its basic function is to execute a certain command repeatedly at a specified frequency. With this command, we can call several commands repeatedly to monitor the server.

By default, the watch command is executed every two seconds, but we can use the -n option to specify the frequency. For example, if we want to execute date every five seconds, we can do this:

$ watch -n 5 date
Copy the code

A server must be used by many people, especially the department’s friends. We can use some commands to monitor them.

We can execute the who command every 10 seconds to see who is using the server.

$watch -n 10 who Every 10.0s: Who butterfly: Tue Jan 23 16:02:03 2019 SHS :0 2019-01-23 09:45 (:0) Dory PTS /0 2019-01-23 15:50 (192.168.0.5) Alvin PTS /1 2019-01-23 5pts shark /3 2019-01-23 11:11Copy the code

If the system is running slowly, you can call the uptime command to check the load average of the system.

$watch uptime Every 2.0s: Uptime Butterfly: Tue Jan 23 16:25:48 2019 16:25:48 up 22 days, 4:38, 3 Users, Load Average: 1.15, 0.89, 1.02Copy the code

Some key processes must not be suspended. Otherwise, services may be affected. Therefore, you can repeatedly count the number of all processes on the server.

$ watch -n 5 'ps -ef | wc -l'Every 5.0 s: ps - ef | wc-l                   butterfly: Tue Jan 23 16:11:54 2019

245
Copy the code

To dynamically check server memory usage, run the free command repeatedly.

$watch -n 5 free -m Every 5.0s: free -m butterfly: Tue Jan 23 16:34:09 2019 total used free shared buff/cache available Mem: 5959 776 3276 12 1906 4878 Swap: 2047 0 2047Copy the code

Not only that, of course, but we can call many commands repeatedly to monitor key server parameters,

2. top

Using the top command, we can know many key parameters of the system, and it is dynamically updated. By default, top monitors the overall state of the system, and if we only want to know how one person is being used, we can use the -u option to specify that person.

$top-U alvin top-16:14:33 Up 2 days, 4:27, 3 Users, Load Average: 0.00, 0.01, 0.02 Tasks: 199 total, 1 running, 198 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0us, 0.2SY, 0.0Ni, 99.8ID, 0.0wa, 0.0Hi, 0.0Si, 0.0ST MiB Mem: 5959.4 Total, 3277.3 Free, 776.4 Used, 1905.8 Buff/Cache MiB Swap: Total 2048.0, 2048.0 free, 0.0 Used.4878.4 Avail Mem PID USER PR NI VIRT RES SHR S %CPU % Mem TIME+ COMMAND 23026 Alvin 20 0 46340 7820 6504 S 0.0 0.1 00:00.05 systemd 23033 alvin 20 0 149660 3140 72 S 0.0 0.1 00:00.00 (SD-PAM) 23125 alvin 20 0 63396 5100 4092 S 0.0 0.1 00.00.00 SSHD 23128 alvin 20 0 16836 5636 4284 S 0.0 0.1 00.00.03 ZSHCopy the code

In this result, you can see not only the number of processes that alvin has run, but also the system resources (CPU, memory) consumed by each process, as well as the key parameters of the entire system.

3. ac

If you want to know how long it takes each user to log on to the server, you can use the ac command. This command requires you to install the ACCT package (Debian) or PSACCt package (RHEL, Centos).

If we want to know the sum of the time taken by all users to log in to the server, we can simply run the AC command without any parameters.

Ac total $1261.72Copy the code

If we want to know how long each user spends, we can add the -p option.

$AC-P shark 5.24 ALvin 5.52 SHS 1251.00 Total 1261.76Copy the code

You can also add the -d option to see the total amount of time a user spends on the server per day.

$ ac -d| tail - 10 Jan 11 total Jan 12 Jan 13 total total 1.36 0.05 16.39 55.33 Jan Jan 15 total 16 total 38.02 Jan 17 total 28.51 Jan 19 Total 48.66 Jan 20 Total 1.37 Jan 22 Total 23.48 Today Total 9.83Copy the code

summary

We can use many commands to the running state of the monitoring system, this paper mainly introduced the three: watch command allows you to repeat a command to monitor the change of some parameters, the top command to view a user the number of running processes, and the consumption of resources, while ac command can view each user using the server time. Which command do you use most often? Welcome to comment!