Process Management Basics

In this post, we’ll cover the basics of process management using a few utilities and commands.

Let’s start with one of the most [arguably] useful utilities, htop. htop is similar to a utility you may already be familiar with, namely top, but htop offers a number of additional features such as vertical and horizontal scrolling, the ability to search for and kill processes without using their process IDs.

Most distros will have top already installed, but htop will need to be installed.

sudo apt-get update
sudo apt-get install htop

Once htop is installed, to run it, you simply run htop in a terminal:

htop

Which will give you a screen that is laid out as shown below:

htop

htop

As you can see, there are four main sections to the utility:

  • The upper-left contains a graphical display for the usage of each processor core on your computer as well as the total physical memory and swap space in use
  • The upper-right contains general usage statistics such as the number of tasks, load average, uptime and time of day
  • The middle section is devoted to the individual processes. It is here that you can scroll up/down and left/right using the arrow keys to view details for each of the  processes running on your computer
  • The lower section is only a single line but displays what each of the function keys do. Note that each of the function keys [F1-F10] also have key equivalents which can be found in htop‘s man page

There are two functions that you will most likely use more than all others, those are the search, [F3] or keys, and the kill, [F9] or k keys.

As an example, say you want to kill a frozen instance of Firefox. Simply search for the process

/ firefox

and kill the process with k. Quite easy, huh?

The next two functions you will probably use the most are the Nice -/+ functions which are accessible through the [F7] or ] and [F8] or [ keys respectively. These control the nice levels of the process (duh!). The nice level of a process is a numerical measure given to the priority of the process. 20 (the largest number) indicates the process has the lowest priority, while -20 (the smallest number) indicates the process has the greatest priority.

The last thing I want to mention about htop is that if a process is owed by root or another user or you want to increase a process’s priority, you will need to run htop as the superuser and work from there:

sudo htop

Now that we’ve covered htop, it’s useful to see how most of that utility’s functions can be done via the command line so you can write shell scripts or manage the processes if you don’t or can’t use htop.

To find a process, you use the command ps. I find the easiest way to use ps is to pass it the options -ef which lists every process on the system using standard syntax and pipe that into grep as shown below using the vlc process as an example:

ps -ef | grep -i [v]lc

In the above command, the -i switch ignores case and the brackets around the first letter of the process vlc supress the output of the grep search process itself as shown below:

Output without brackets

Output without brackets

Output with brackets

Output with brackets

To kill the process, you can either use kill if you know the PID, or killall or pkill if you know the process name. All of the below examples would kill the running vlc process (note in the last example the user running the process vlc on this computer is aeryn):

kill -9 30941
killall -9 vlc
pkill -KILL vlc
pkill -KILL -u aeryn vlc

When writing a script, I sometimes find it useful to wrap ps with kill to search for and kill the process that way. Again, the example below will find all processes matching the term vlc, print the process ID belonging to each and pass those arguments to kill which issues a SIGKILL to each.

ps -ef | grep -i [v]lc | awk '{print $2}' | xargs kill -9

The number 9 you keep seeing corresponds to SIGKILL. A list of common ones is below:

  • SIGHUP  (1)  Hangup detected on terminal
  • SIGINT  (2)  Interrupt from keyboard
  • SIGKILL  (9)  Kill signal
  • SIGCONT  (18)  Continue stopped process
  • SIGSTOP  (19)  Stop running process

Finally, to manually change the nice value (remember that the higher the number the lower the process priority), you use the command nice. The command below would add 10 to the nice value (each process by a normal user defaults at 0, so adding 10 would put it at +10) of the spawned backup process tar (for example if you are scheduling backups but want the backups to not interrupt the normal operation and load on your server)

nice -n 10 cvzf server_backup.tar.gz /
Tagged , , , , , , . Bookmark the permalink.

One Response to Process Management Basics

  1. Wesley says:

    I don’t see many commenters here, it means you don’t get many visits. I know how to get laser targeted traffic from the largest traffic sources in the web and make your site go viral. If you want to know just search in google for: Juuri13 SEO advice

Leave a Reply

Your email address will not be published.

Protected by WP Anti Spam