Zac Fukuda
049

15 linux commands to monitor server

The part of developer’s job is monitor servers. Here are fifteen Linux commands to do the job.

Test Environment

The output of commands shown below are results run on the following environment:

  • VPS: AWS EC2 t4.micro
  • OS: Ubuntu 22.04.3 LTS

On Ubuntu 22.04, I needed to install the new packages to run numactl, iostat, and netstat:

$ sudo apt install numactl
$ sudo apt install sysstat
$ sudo apt install net-tools

ps

Meaning “process status.” Displays the status of running processes.

$ ps
 PID TTY          TIME CMD
1328 pts/0    00:00:00 bash
1777 pts/0    00:00:00 ps

PID means process ID, TTY computer terminal by which the process is run, TIME the time that the process is running, CMDthe command that triggered the process.

kill

Sends the signal to control the target process. Run this command fully understanding the control of process never breaks the operating system.

You can see the list of available signal types:

$ kill -l
1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

vmstat

Meaning “virtual memory statistics.” Displays the various information of system.

$ vmstat
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  43696   3760 224360    0    0  2947  1819  363  490 11  6 81  2  0

See the more detail at Linux commands: exploring virtual memory with vmstat.

getconf

Meaning "get the configuration." Displays the given configuration variable.

$ getconf PAGESIZE
4096

numactl

Meaning “NUMA control.” Controls NUMA policy for processes or shared memory.

$ numactl --hardware
available: 1 nodes (0)
node 0 cpus: 0 1
node 0 size: 419 MB
node 0 free: 64 MB
node distances:
node   0 
  0:  10

free

Displays the memory usage. You can give the either of k,m,g option to see the size in KB, MB, GB.

$ free -m
               total        used        free      shared  buff/cache   available
Mem:             419         187           5           0         226         213
Swap:              0           0           0           0           0           0

swapon/swapoff

Does enable/disable swapping.

$ swapon -a
$ swapoff -a

stat

Displays the meta information(inode) of file.

$ echo "Hello, world" > hello-world.txt
$ stat hello-world.txt
  File: hello-world.txt
  Size: 13              Blocks: 8          IO Block: 4096   regular file
Device: 10301h/66305d   Inode: 259849      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/  ubuntu)   Gid: ( 1000/  ubuntu)
Access: 2023-11-19 07:01:45.260376664 +0000
Modify: 2023-11-19 07:01:45.260376664 +0000
Change: 2023-11-19 07:01:45.260376664 +0000
 Birth: 2023-11-19 07:01:45.260376664 +000

df

Meaning “disk free.” Displays the usage of disk space for file systems.

$ df -kh
Filesystem       Size  Used Avail Use% Mounted on
/dev/root        7.6G  1.7G  6.0G  22% /
tmpfs            210M     0  210M   0% /dev/shm
tmpfs             84M  984K   83M   2% /run
tmpfs            5.0M     0  5.0M   0% /run/lock
/dev/nvme0n1p15   98M  6.3M   92M   7% /boot/efi
tmpfs             42M  4.0K   42M   1% /run/user/1000

$ df -ikh
Filesystem      Inodes IUsed IFree IUse% Mounted on
/dev/root        1013K   74K  940K    8% /
tmpfs              53K     2   53K    1% /dev/shm
tmpfs             800K   618  800K    1% /run
tmpfs              53K     3   53K    1% /run/lock
/dev/nvme0n1p15      0     0     0     - /boot/efi
tmpfs              11K    25   11K    1% /run/user/1000

cat

Meaning “concatenate.” Displays the file content. To display the CPU information:

$ cat /proc/cpuinfo
processor       : 0
BogoMIPS        : 243.75
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
CPU implementer : 0x41
CPU architecture: 8
CPU variant     : 0x3
CPU part        : 0xd0c
CPU revision    : 1

processor       : 1
BogoMIPS        : 243.75
Features        : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm lrcpc dcpop asimddp ssbs
CPU implementer : 0x41
CPU architecture: 8
CPU variant     : 0x3
CPU part        : 0xd0c
CPU revision    : 1

w

Meaning “who.” Displays who is logged in the computer, what they are doing.

$ w
  07:16:53 up  1:07,  2 users,  load average: 0.07, 0.02, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ubuntu   pts/0    103.5.140.163    06:14   47:22   0.03s  0.03s -bash
ubuntu   pts/1    103.5.140.163    06:36    1.00s  0.05s  0.00s w

top

Displays the real-time statistics of computer and processes.

$ top
top - 07:55:07 up 9 min,  1 user,  load average: 0.97, 0.66, 0.30
Tasks: 131 total,   1 running, 130 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :    419.1 total,    104.9 free,    153.8 used,    160.4 buff/cache
MiB Swap:      0.0 total,      0.0 free,      0.0 used.    247.0 avail Mem 
  
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  14076 ubuntu    20   0   10372   2560   1920 R   0.3   0.6   0:00.03 top
      1 root      20   0   20204   7680   4096 S   0.0   1.8   0:04.23 systemd
      2 root      20   0       0      0      0 S   0.0   0.0   0:00.00 kthreadd
      3 root       0 -20       0      0      0 I   0.0   0.0   0:00.00 rcu_gp

iostat

Meaning “input/output statistics.” Displays the input/output statistics.

$ iostat
Linux 6.2.0-1012-aws (ip-xxx-xx-xx-xx)  11/19/23        _aarch64_       (2 CPU)

avg-cpu:    %user   %nice %system %iowait  %steal   %idle
             0.41    0.08    0.20    0.16    0.00   99.15
  
Device             tps    kB_read/s    kB_wrtn/s    kB_dscd/s    kB_read    kB_wrtn    kB_dscd
loop0             0.04         1.38         0.00         0.00       5941          0          0
loop1             0.00         0.01         0.00         0.00         25          0          0
loop2             0.06         0.44         0.00         0.00       1904          0          0
loop3             0.01         0.01         0.00         0.00         54          0          0
loop4             0.26         9.67         0.00         0.00      41734          0          0
loop5             0.00         0.00         0.00         0.00         14          0          0
nvme0n1           7.62       266.73       193.20         0.00    1151658     834169          0

See the more detail at iostat command in Linux with examples

netstat

Meaning “network statistics.” Displays the network connections.

$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0     80 ip-111-11-11-11.ec2:ssh 111-111-111-111.w:60432 ESTABLISHED
Active UNIX domain sockets (w/o servers)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  3      [ ]         STREAM     CONNECTED     95063    
unix  3      [ ]         STREAM     CONNECTED     17168    /run/dbus/system_bus_socket

dmesg

Meaning “display kernel-related messages.” Displays the kernel ring buffer.

$ dmesg
[    0.000000] Booting Linux on physical CPU 0x0000000000 [0x413fd0c1]
[    0.000000] Linux version 6.2.0-1012-aws (buildd@bos01-arm64-014) (aarch64-linux-gnu-gcc-11 (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #12~22.04.1-Ubuntu SMP Thu Sep  7 16:00:15 UTC 2023 (Ubuntu 6.2.0-1012.12~22.04.1-aws 6.2.16)
[    0.000000] efi: EFI v2.70 by EDK II
[    0.000000] efi: SMBIOS=0x4bed0000 SMBIOS 3.0=0x4beb0000 ACPI=0x486e0000 ACPI 2.0=0x486e0014 MEMATTR=0x4a777018 MOKvar=0x4be90000 RNG=0x41c10018 MEMRESERVE=0x48566298 
[    0.000000] random: crng init done
[    0.000000] secureboot: Secure boot disabled
[    0.000000] ACPI: Early table checksum verification disabled
…