Do you think your Linux computer will automatically take care of everything by itself? Sometimes you may need to step in directly.
Process management is a case in point. Linux creates a process whenever a program is launched, either by you or by Linux. This process is a container of information about how that program is running and what’s happening.
If the process runs and terminates correctly, then everything is hunky-dory; however, if it hogs the CPU, or refuses to go when its time is up, then the Linux commands described below may help you to restore law and order.
Let’s start with a list of things you may want to do when managing Linux processes:
- See which processes are running
- See how much of your Linux system the processes are using (especially any greedy ones)
- Locate a particular process to see what it’s doing or to take action on it
- Define or change the level of priority associated with that process
- Terminate the process if it has outlived its usefulness or if it’s misbehaving
The commands described below should be entered via the command line interface. Simply open a terminal (all-text) window to access this interface. It may look basic, but it’s actually very powerful and flexible – just the thing for keeping all those processes in line.
The top command gives you information on the processes that currently exist. As the sample output above shows, the first part of the information is an overview of the situation.
The second part, organized in columns, gives details for each process, including its unique reference number (PID), priority (PR), status (S), and resource usage (%CPU, for example).
The htop command is like top, but prettier and smarter. The information is presented in a clearer format, and you can select a particular process (use the arrow keys) and then act on it (use the F1, F2, etc. keys) with the htop display.
So why would anyone use anything other than htop? Simply because htop is not always available by default on Linux systems (whereas top is always available).
You may have an extra installation step before you can use it. Your installation instruction will be sudo apt-get install htop if you’re using Ubuntu or Debian, for instance.
The colors htop uses in its display help convey its message.
The CPU and memory bars may show blue bars for low priority processes, green for normal priority, or red for kernel. Yellow corresponds to IRQ (interrupt request) time, magenta to soft IRQ time, and gray to I/O (input/output) wait time.
The load average figure represents the degree to which the CPU is being kept busy. A figure of “1.0” corresponds to 100 percent busy. The figure of “0.37” in the screenshot below corresponds to 37 percent.
This is the load average over the last minute. The two other load average figures are the load averages over the last five and the last 15 minutes, respectively.
Use the ps command to list running processes (top and htop list all processes whether active or inactive). You’ll need to specify one or the other option to get useful information, however.
The command ps -a will list all the processes on your system. The command ps -a | grep mysqld would then pick out the mysqld process if, for example, you had a connection via the system to a MySQL database.
A step up from the simple ps command, pstree is used to display a tree diagram of processes that also shows relationships that exist between them.
Every process is generated, or spawned, by another process (a parent process) in Linux. What’s important to know is that if you alter something for a parent process, you affect the child processes as well.
In particular, if you stop the parent, you automatically stop the children!
The who command will display a list of all the users currently logged into your Linux system. If that’s just you and the operating system itself, then you have a free hand to manage processes as you like.
On the other hand, if other (human) users are also logged in, take caution before altering or terminating processes that may be important for them.
You can also use the users command for simpler information, the whoami command to tell you who you are (as perceived by the system), and the w command, which gives you not only the names of the users, but information on what they are currently doing.
As its name suggests, kill can be used to terminate a process with extreme prejudice. Although in a more general sense, its function is to send signals (one of which is “die!”).
It allows you to stop individual processes or groups of processes without having to entirely stop or reboot your Linux system — something other users may also appreciate.
Let’s suppose your mysqld process is misbehaving. As you know from your top display, this process (in our example above) has a PID of 5979. To terminate this process, you could type in:
If for any reason this is not enough, there are more aggressive options available. An example would be the -9 option (also known as -KILL):
kill -9 5979
kill -KILL 5979
If you don’t know the PID or you want to take a shortcut (just make sure it’s the right one), the killall command will let you terminate all instances of a process with the same name. To kill all the processes called mysqld, for example, type:
killall -9 mysqld
If kill seems just too unfriendly, you might like to first try being “nice.” The nice command lets you define the priority of a process before you run it (use renice for a process that’s already running).
The commands described above are just a small part of everything the command line interface has to offer.
You can slice and dice your Linux process management in countless ways, thanks to the different command options and the ability to combine several commands at once.
By trying them out for yourself, you’ll see which commands are the most useful for you. Just remember to think about their effect before you put them into action.
Photo Sources: extensionengine.com