Process management with Linux

In this column, we examine some tools to assist with process management. Essentially, a process is a program running in a Linux system, and process management is the task of looking after the running processes. Normally a user would not have to worry about process management, but what happens if a program locks up, using all of the CPU, or if your system runs out of memory? This is where process management can help you improve the performance of your system and avoid unnecessary reboots.

The ps and top commands

The ps command displays a list of running processes and some statistics on their use of system resources. A large number of filters can be applied to the ps command to display different information and to set the scope of processes to be listed.

To try out the ps command, open a shell and type the following commands:

$ ps

$ ps aux

The first command will show a list of running processes belonging to the current user (i.e., you). This list will be quite short and only contain the process id (PID) and the process name. The PID is a unique number assigned to each process and is used to control the process with some of the tools we will examine further down.

The second command will show a much more detailed list containing a much larger number of processes. It covers all of the processes currently running in your Linux system and information listings report on system areas such as the current CPU and memory usage. This command is very helpful when looking for programs that hog memory or CPU time.

The top command provides a live interactive interface to the list of running processes, the format of which is very similar to that of ps aux. The top command refreshes statistics on the state of processes in your system every couple of seconds. To try it, in a shell type:

$ top

The screen displayed will show the overall state of your system on the first two lines and a list underneath of processes ordered by highest CPU usage. You can reorder this list by highest memory usage by pressing <M>; to return to ordering by CPU usage, press <C>. With the top command running you can observe the internals of your running system. Try moving your mouse around, or loading a Web page - notice how the X server process will suddenly jump in usage while the task is being carried out and then drop back down when it is completed?

The kill command

Now that you have spent some time observing processes running, it's time to look at the management side of process management. The kill command can be used in conjunction with a process's PID to end a currently running process, which can be very handy if a running program that uses a lot of your CPU suddenly locks up. To free up your system, first look up the process using ps or top, and then kill it using the kill command followed by the PID. For example, if your Web browser has gone haywire and is not responding, type:

$ ps aux | grep mozilla

which outputs:

od 17007 99.9 8.7 82592 56264 ? S 09:35 0:00 mozilla-bin

You can see the CPU usage for mozilla-bin is 99.9 per cent of the CPU. To kill Mozilla, type:

$ kill 17007

The first line pipes the ps output into the grep command, which filters the ps output to only display lines containing "mozilla". This makes it much easier to locate the PID for a process. The second instruction issues a kill command to the mozilla process. Sometimes, however, a process will not respond to the kill command and will refuse to end. To force a program to end, add the "-9" switch to your kill command:

$ kill -9 17007

This command gives the process no choice and forces it to exit immediately - however, it may damage the program as it will not have a chance to exit cleanly, saving configuration files, etc. In most cases, the "-9" switch should only be used as a last resort.

Join the newsletter!

Or

Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Alastair Cousins

PC World
Show Comments

Brand Post

Imou: At home with security

Modern living is all about functionality and security for everybody from the very young to the very old. With Imou anybody can enjoy smart life – the solution is at their fingertips.

Most Popular Reviews

Latest Articles

Resources

PCW Evaluation Team

Tom Pope

Dynabook Portégé X30L-G

Ultimately this laptop has achieved everything I would hope for in a laptop for work, while fitting that into a form factor and weight that is remarkable.

Tom Sellers

MSI P65

This smart laptop was enjoyable to use and great to work on – creating content was super simple.

Lolita Wang

MSI GT76

It really doesn’t get more “gaming laptop” than this.

Jack Jeffries

MSI GS75

As the Maserati or BMW of laptops, it would fit perfectly in the hands of a professional needing firepower under the hood, sophistication and class on the surface, and gaming prowess (sports mode if you will) in between.

Taylor Carr

MSI PS63

The MSI PS63 is an amazing laptop and I would definitely consider buying one in the future.

Christopher Low

Brother RJ-4230B

This small mobile printer is exactly what I need for invoicing and other jobs such as sending fellow tradesman details or step-by-step instructions that I can easily print off from my phone or the Web.

Featured Content

Product Launch Showcase

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?