4. Manipulating processes in Linux

As you work more with linux you will have occasion to monitor the number of running programs (processes) on the machine you are logged into. You may be curious, for example, about how much cpu and memory they are consuming. You may also be curious about resource consumption for other people’s programs. The general purpose process status command ps is the tool for this task. For example, issue ps with the (almost) all processes -a and all users -u switches:

% ps -au

You should see something like:

_images/ps.png

Firstly, ignore the warning on the first line. ps is on of those weird linux commands that accepts 2 ways of formatting switches. If it bothers you, you can remove the dash from the command line and rerun. (But then the command’s format is different from what I have been telling you about switches and more confusion will result.) The data returned is in tabular format so read the column headings. Looking at the first column we see that user “setiawan” is one busy user. You also see the user ‘root” and the user “coan” (me). You will undoubtedly see your username. The next column labels the process id (PID) of the listed processes. This is a unique numerical identifier. Next are “%CPU” and “%MEM” showing the fraction of cpu and memory consumed by a given process. These are important because if the machine is running slowly, it is often due to these fractions being high. Skip the next 4 columns until you come to the 2 columns “START” and “TIME”. These guys tell you when you started the given process. Finally, we come to the last column, “COMMAND” which lists the actual process name.

If you only want to see just your processes, you have at least 2 choices. You can be lazy and just type:

% ps

Or, if you want more information, you can use a pipe:

% ps -au | grep coan

where I used my username as an example. I will leave it to you and the man pages to see how to get a long listing from ps using one of its many switches.

Sometimes you launch a process (program) that consumes so much of the cpu that it dramatically slows the machine so that useful work ceases. You issue the ps command and verify that that the process is indeed a cpu hog. Rather than wait for the process to end, you can hasten its demise. This is done using the kill command and the PID of the process. We’ll see an example of this shortly.

Linux considers user processes to be in one of 3 states: foreground, background and suspended. A foreground process is one that is executing in the window you typed it in and you cannot get the cursor back until ]the process completes. This is the way you have been running up until now (with the exception of gedit.) As you have already seen, you can run a process behind the scenes, so to speak, as you did with gedit. Finally, there is a sort of hibernation state – the suspended state. Here the process is still “living” but is not actively executing. You are able to modify only those processes that you initiated (“own” is the jargon).

To see process suspension in action, consider first the useful linux command sleep that delays the execution start time of a command by some number of seconds that the user specifies:

% sleep 10
% ls

Note that the cursor does not return until 10 seconds have elapsed and only then does ls execute.

If you now issue:

% sleep 100

and then type Ctrl-Z (holding down simultaneously the Ctrl key and the “z” key), you will suspend the sleep process. Try It! You should see:

Suspended

This sleep` process didn’t die, it’s, well, suspended. If you next type the jobs command, linux will tell you about suspended processes (as well as any background process running)

% jobs

and linux will reply with:

[1] + Suspended    sleep 100

The number in brackets [] tells you the process’ “job number.” To push this job to the background where it will continue to run, issue:

% bg %1

If you only have 1 suspended process, you can just type

% bg

and the process will be “backgrounded.”

To bring a suspended process to the foreground, use the foreground command fg and the process’ job number:

% fg %1

The process then starts executing as if you had never suspended it. Again, if you have only 1 suspended process, you can “foreground” it by issuing

% fg

You can delete a suspended process by issuing the kill command along the process’s job number. Try it wilh sleep 100:

% kill %1

By the way, if you just want to kill a process running in the foreground and not even bother to put it in the background, you can issue a “Ctrl-C” command (hold down simultaneously the Ctrl key and the “c” key). Try it with the sleep 100. you should see the cursor return immediately.

Another way of killing processes is to issue kill` with the PID of the offending process. Linux has a command ``xclock to produce a clock face on your desktop so you can watch the time. Launch it so it runs in the background:

% xclock &

Find xclock‘s PID. Assuming its PID is 87912, issuing:

% kill -9 87912

will eliminate the process and the clock face will disappear. (The number 9 is a bit obscure but always works. Leaving off the 9 sometimes does not work. Read more on Linux/Unix.) Were you successful?

Finally, there is a kind of symmetry between foreground jobs and background ones. We saw how to move foreground jobs to the background. we can do the reverse. Consider the case of gedit:

% gedit &
% jobs

Assuming its job number is 1, you can bring gedit from the background to the foreground using the foreground command fg:

% fg %1

You can now kill it off by using one of the techniques in this chapter. Were you successful?

4.1. Summary

Command Meaning
ps -au list all current processes
command & run command in the background
jobs list current jobs
bg %1 push job number 1 to the background
fg %1 bring job number 1 to the foreground
kill %1 kill process with job number 1
kill -9 2132 kill process with PID 2132
Ctrl-C kill the process running in foreground
Ctrl-Z suspend the process running in foreground
sleep 15 delay execution for 15 seconds
xclock place a running clock on your desktop

Table Of Contents

Previous topic

3. Examining file content, redirection and pipes

Next topic

5. “Dot” files and key shell variables

This Page