Processes
Getting to know the different PS Commands
The ps command may be used to monitor executing processes. ps is short for Process Status. This command may be used to obtain the ids of processes that are hanging or taking up a lot of memory so that they may be terminated. The processes of all current users may also be obtained using the 'aux' option of the ps command. The following commands demonstrate using ps to display jobs of the current user on the current shell and using ps to display all the jobs of the user across all shells and terminals along with CPU consumption (the latter is similar to viewing the task manager using Ctrl-Alt-Del on the Windows OS). This user has a number of Internet browsers and two bourne shell terminals open; he is editing a file on one shell using emacs and has issued the ps command from the other shell.
[ LinuxUser ] ~$ ps PID TTY TIME CMD 3511 pts/1 00:00:00 bash 3514 pts/1 00:00:00 ps [ LinuxUser ] ~$ ps ux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND LinuxUser 715 0.3 4.8 31096 18820 ? S 13:35 0:03 /usr/lib/mozilla/ LinuxUser 723 0.0 4.8 31096 18820 ? S 13:36 0:00 /usr/lib/mozilla/ LinuxUser 724 0.0 4.8 31096 18820 ? S 13:36 0:00 /usr/lib/mozilla/ LinuxUser 725 0.0 4.8 31096 18820 ? S 13:36 0:00 /usr/lib/mozilla/ LinuxUser 735 0.0 4.8 31096 18820 ? S 13:36 0:00 /usr/lib/mozilla/ LinuxUser 753 0.0 0.3 2108 1160 pts/0 S 13:36 0:00 bash LinuxUser 787 0.7 1.7 9520 6784 ? S 13:50 0:00 emacs LinuxUser 789 0.2 0.3 2112 1164 pts/1 S 13:51 0:00 bash LinuxUser 794 0.0 0.4 3560 1576 pts/1 R 13:51 0:00 ps ux
The PID column contains the process ID. A running process is also called a job. The terms process and job are interchangeable. However, a process is usually referred to as a job when used in conjunction with job control, a shell feature that allows users to switch between processes. Since job control is a shell feature, the implementation of job control varies among shells.
Typically, users run only a single job at a time - the command issued at the shell prompt. Job control allows users to run several jobs at once. Some jobs - compiling programs, compressing or converting large files etc. - take a long time to complete. Running such jobs in the background allows the user to continue interacting with the system. One way to run a process in the background is to append an '&' character to the end of the command.
[ LinuxUser ] ~$ javac Java/src/myProgram.java &
At any time, only one job may run in the foreground while any number of jobs may be relegated to the background. Jobs may also be temporarily stopped or suspended. Suspended jobs may be resumed in the foreground or background using the 'fg' or 'bg' commands. Resuming a suspended job does not change the internal state of the job - it continues to run where it left off. One way of suspending a job is to type Ctrl-z in the shell where the process is executing
The example below demonstrates job suspension and resumption on the foreground. As soon as the ls -l command is issued in a large directory, Ctrl- Z is typed. This suspends the job. This action also prints out job related information (job number etc.). The job we just stopped corresponds to job number 1 (the initial number in square brackets.). Note that the ps entry for the job contains an 'S' in the first column to indicate that it is suspended. The fg command with the argument %<job_id> is issued to resume the process in the foreground; this causes the directory listing to continue.
[ LinuxUser ] /bin$ ls -l | more [1]+ Stopped ls -l | more [ LinuxUser ] /bin$ ps PID PPID PGID WINPID TTY UID STIME COMMAND 3564 1 3564 3564 0 1004 18:05:28 /usr/bin/bash S 1224 3564 1224 3208 0 1004 03:53:44 /usr/bin/ls S 3980 3564 1224 3824 0 1004 03:53:44 /usr/bin/more 612 3564 612 2468 0 1004 03:53:49 /usr/bin/ps [ LinuxUser ] /bin$ jobs [1]+ Stopped ls -l | more [ LinuxUser ] /bin$ fg %1 ls -l | more total 100518 lrwxrwxrwx 1 LinuxUser Users 12 Nov 21 00:30 X11 -> ../X11R6/bin -rwxr-x---+ 1 LinuxUser Users 29696 Oct 25 09:43 [.exe -rwxr-x---+ 1 LinuxUser Users 117760 Dec 30 2005 a2p.exe -rwxr-x---+ 1 LinuxUser Users 94232 May 5 2005 a2ping -rwxr-x---+ 1 LinuxUser Users 67072 Dec 16 2002 addftinfo.exe -rwxr-x---+ 1 LinuxUser Users 481280 Aug 17 21:58 addr2line.exe
The following example demonstrates suspending the directory listing job and starting it in the background. When the job is started in the background, its output is sent to /dev/null (this is the equivalent of a trash can). To relegate a foreground job to the background, the user may suspend the job and use the 'bg' command to start it once more in the background.
[ LinuxUser ] /bin$ ls -l | more [1]+ Stopped ls -l | more [ LinuxUser ] /bin$ jobs [1]+ Stopped ls -l | more [ LinuxUser ] /bin$ bg %1 > /dev/null [ LinuxUser ] /bin$
Slow jobs or hanging processes may be interrupted or killed by either typing Ctrl-C in the shell where the job is executing or issuing a kill command directed to the process ID. Some programs do not respond to Ctrl-C; a kill command may be used to interrupt such processes. The following command kills a non-responsive Internet Browsing window. Adding the -9 option makes sure that the interrupt cannot be caught or ignored by the running program; it is a 'sure kill'.
[ LinuxUser ] ~$ ps ux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND LinuxUser 715 0.3 4.8 31096 18820 ? S 13:35 0:03 /usr/lib/mozilla/ LinuxUser 723 7.0 9.8 31096 18820 ? S 13:36 0:00 /usr/lib/mozilla/ LinuxUser 724 0.0 4.8 31096 18820 ? S 13:36 0:00 /usr/lib/mozilla/ LinuxUser 725 0.0 4.8 31096 18820 ? S 13:36 0:00 /usr/lib/mozilla/ LinuxUser 735 0.0 4.8 31096 18820 ? S 13:36 0:00 /usr/lib/mozilla/ LinuxUser 753 0.0 0.3 2108 1160 pts/0 S 13:36 0:00 bash LinuxUser 787 0.7 1.7 9520 6784 ? S 13:50 0:00 emacs LinuxUser 789 0.2 0.3 2112 1164 pts/1 S 13:51 0:00 bash LinuxUser 794 0.0 0.4 3560 1576 pts/1 R 13:51 0:00 ps ux [ LinuxUser ] ~$ kill -9 723
The nice command is used to alter an initial job priority. The lower the nice value the higher the priority. The typical range is -20 (highest) to 19 (lowest). Using nice is quite simple. The following command sets the compilation of a Java program to a high priority
[ LinuxUser ] ~$ nice -n 5 'javac Java/src/myProgram.java'
The renice command may be used to alter a running process' priority. Normal users may only use the range 0-20 for their own jobs; only the superuser may alter priorities of other user's jobs. Renice may be used with a process ID or a user ID. The former changes the priority of a single process while the latter changes the priority of all the currently executing processes of a user.
# renice 5 -p 10023 # renice 12 -u LinuxUser