Or How I Learned To Stop Worrying And Love top
First of all, if you’re running Linux on your laptop and a media server in your house or you’re the admin of a small number of servers that you have complete control over then this article is not aimed at you. You can configure your systems however you want. I won’t judge.
This article is aimed at admins that work in larger organizations where there exists a heterogeneous Linux environment i.e. servers, VM’s, containers, networking gear etc, that are running different distros at different versions with different policies. The normal way that things happen in a Linux based organization. This article is aimed at admins starting out in there.
The purpose of this article is to try to explain why becoming dependant on specialised tools is going to make your life harder. By “specialised tools” here I mean packages that are not amongst the default packages on most distros.
The sorts of tools I have in mind are:
htop
and glances
are great tools. They provide lots of information very clearly and enable debugging an issue much more easily.
The problem and the reason for this article is that in all likelihood they won’t be there when you need them. Sure you could install them. But frequently this is not possible (are they in the repos? Am I allowed to? Is the disk thrashing so much that it would take half an hour? etc etc etc).
So What Should You Do?
Train yourself to use the tools that you will find on every Linux system AND get used to reading their less pretty output. The following is a short list of the tools that will give (most of) the same information and you will always find on every linux server.
Process information, load, memory use, CPU usage
The tool here is top
. It displays a ton of system metrics but requires a little familiarity to use rapidly. The easiest way to do that is to start using in place of htop
or similar tools every day.
top
has some interactive commands i.e. that work while it is running. The most useful of these are:
m
- This command will order the processes by their memory usage (instead of CPU usage)1
- This command will show every CPU core use rather than the system aggregate. Then pressingt
will show this info in bar charts.k
- This command will allow you to kill a process. The default on pressingk
is to select the top process i.e. the one using the most CPU or memory if that is how the processes are ordered.e
andE
- These commands will changes the units that data is show i.e. Bytes, Megabytes, Gigabytes etc, in in the top information bar and the process list.
Disk Use
The df
command will give you information on all the system’s filesystems:
df -h
Alternatively, lsblk
will also give the same information but is easier to read and nicely formatted.
If you need to know which directory under the current one is using the most disk space use du
piped in sort -h
to get the results ordered by size:
du -hs * | sort -h
System IO
The command vmstat
will show the amount of data being read and written to the block devices as well as swap along with CPU usage and IO wait. It is most usefully run as a continuous report. The following gives updates every 5 seconds:
vmstat 5
Aliases
The other specialised tool which causes the same issues are bash aliases with replace common commands. The most frequent that I have seen is aliasing ll
with ls -la --color=auto
.
Once you commit ll
to muscle memory then the first thing you will see whenever you want a directory listing on a server without the alias set will be:
[~]$ ll
bash: ll: command not found
Then you’re going to have to run ls -la
anyway. After you have typed ls -la
a few thousand times you will be able to do it almost as rapidly as ll
.
The output isn’t as pretty but all the information is there and you will get faster at parsing it the more you use it.
Aliases are really useful when you have a long, custom command that is not a replacement for a common command.