9. Unix processes

Process control

Let’s see the processes that are running on your system:

$ ps -aux

As usual, look at man for the option switches.

You can see how the processes are forked from their parents by adding the f option to our ps command:

$ ps -axf | less -S

which will show a nice tree.

Try using grep via a pipe to get information about a particular process name, note that the grep itself will often match as well. Newer systems may have a command called pgrep, which is neater.

A handy interactive tool for looking at processes is top; you’ll end up using it a lot to work out what’s burning all your CPU or eating all your memory! top will show a regularly updating process list, you get back to the terminal by hitting q.

Processes can either be in the background or foreground. If the latter, the shell will not prompt again until they have finished: try running gedit and then re-focus the shell window. Your typing now isn’t registering as shell commands, it’s just appearing in the window while the editor sits there doing nothing. This is where the run command in the background syntax comes in.

Close gedit with a click (or highlight the terminal and press Ctrl-c to kill the foreground process), then try it again with:

$ gedit &

The terminal might get some messages written to it by gedit, but you should still be able to run commands: gedit is in the background. If you forget to put the & on the end, you can pause the foreground process with Ctrl-z, and then put it into the background with bg. The corresponding command fg will bring the most recent backgrounded command into the foreground. If a foreground process locks up so badly that you can’t even background it, you open another terminal and use a combination of ps and the kill or pkill commands to kill the offending process or its parent.