Subsections

7. Processes

Whenever you start a program from the shell, a new process is created. Sometimes you may want to control how the process is running, and several commands are available for this task.

7.1 Background and foreground

You usually start commands in the foreground, i.e. in such a way that what you type at the keyboard is directly sent to the running process and its output comes back to you (but see 4.3). While this is strictly necessary for some programs you need to interact with (e.g. pine), sometimes you could prefer to start a process in the background, so that you immediatly get back the shell prompt (e.g. you compile a really big project - you will probably redirect output to some file). This is done appending the & operator to the command line: make all >& /tmp/make-all.output &.7.1You get something like [3] 342 and a new command prompt: [3] is the job identifier, and can be used to control the job within the launching shell only; 342 is the process identifier (PID) and can be used system-wide. Once the make is done you will get [3]+ Done make all >& /tmp/make-all.output just before a command prompt.

job control quick reference
command start command in the foreground
command & start command in the background
<ctrl-c> kill current foreground job
<ctrl-z> stop current foreground job
fg continue in the foreground last stopped job
fg %job-id continue in the foreground job job-id
bg continue in the background last stopped job
bg %job-id continue in the background job job-id
kill %job-id kill running job job-id
kill pid kill running process pid (this can be used from other shells)
killall name kill running process(es) named name
<ctrl-s> suspend terminal output
<ctrl-q> resume terminal output

7.2 Nice

Concurrent processes are scheduled to run based on their own priority. You can alter the way your processes are scheduled with the nice and renice commands. You (the user) are only allowed to lower the priority of processes - only the super-user can do the opposite. Low-priority processes will get less CPU time on a busy system, thus allowing other processes to run faster; still they will run at full speed if no other process is in the run queue.

nice quick reference
command start command at normal priority
nice command start command at low priority
renice -p pid lower priority of process pid

7.3 Signals

Running processes can be controlled using signals. Signals are sent using the kill command: kill -signal-number pid or kill -signal-name pid will both send a signal to process pid. The list of available signals is shown with kill -l.

common signals
STOP stops a running process - actually <ctrl-z> sends this signal
CONT continue a stopped process
INT kill a process - actually <ctrl-c> sends this signal
TERM kill a process
KILL kill a process
signal KILL can not be blocked, i.e. the process will surely die

7.4 Diagnostics

A few commands are available to inspect process status.

ps will show almost any possible kind of information of any running process. It has quite a lot of command line options, so please see man ps.

top is an interactive process viewer. Its default configuration will show the «most active» processes running on the system, but this can be changed to show processes from one user only or processes with the largest memory usage. It can be used to send signals or renice processes too. An on-line help is available hitting <h>. Various top-like programs are available in graphic environments (gtop, SystemMonitor, ...); note that these programs need a large amount of resources just to redraw their screen, so their reports may be not so accurate (e.g. SystemMonitor will use from 5 to 10% of CPU time on a P4/2.4GHz).

uptime will show the time since last reboot, the number of users logged in and three load average numbers: these are a moving mean of the system activity during the past 1, 5 and 15 minutes. If you see large values, don't be surprised if the system slows down.

limit will display memory size and CPU time limitation for user processes. This is an internal command of the tcsh shell; the corrisponding for bash is ulimit -a. CPU time limits for user processes are enforced on remote accessible, general purpose machines (currently shannon).



Footnotes

... &.7.1
Analysis of this command (pay attention to the different meanings of &):
  make all start make with parameter all
  >& redirect both stdout and stderr...
  /tmp/make-all.output ...to this file
  & and go to background
Piero Calucci 2004-11-05