Help:Process control
From MMAE
This document explains how to do process control on Unix machines using the ps and kill commands.
Contents |
[edit] Overview
- An executable is a program on disk.
- A process is an instance of a running program. A process is identified by its Process ID or PID
- The ps command lists processes.
- The kill command can kill processes by PID.
- A job is one or more processes in a pipeline running in a shell that supports job control.
- The jobs command lists jobs in the current shell (window).
- The kill command can kill jobs by job number or name (marked by %)
[edit] Background and Foreground
When a process is in the foreground, it is active in your shell and can accept input from the keyboard. When a process is in the background, it may be able to send output to the terminal window, but keyboard actions will go to the current foreground job or your shell. The advantage of putting a program in the background is that you don't have to wait for it to finish, and you can do other things or start other programs.
Note: Most of the time you will still see output of a program when it is in the background.
There are two ways to put a process in the background:
- When you start a program, add a & to the end of the command line.
Example: sleep 1000 &
- Alternately you can put a process in the background after it is already running. While the process is in the foreground you can temporarily stop it with ctrl-z and then type bg to continue the process in the background. Then to bring a process back into the foreground again you type fg in your shell.
[edit] Jobs
The shell keeps track of what program groups have been started in the current window, and assigns each one a job number when it is started. If the command is followed by a & character, the job is put in the background, and the job number is printed.
The jobs command will list jobs in the current window; jobs -l will also show the PID for each process in a job. The job can be referenced by a % followed by the job number. For example:
> sleep 100 & [1] 2354 > jobs [1] + Running sleep 100 > jobs -l [1] + 2354 Running sleep 100 > kill %1 [1] Terminated sleep 100
[edit] Listing Processes
There are two commands that can be used to get the list of running processes on the machine.
The first is ps. ps is a program to report status of specific processes. When getting process information you will generally see four columns. The first column shows the PID (Process ID #) of the process. The second number is the screen that the process is attached to. The third column is how much CPU time the process has used. Finally, the fourth column is the name of the process.
There are various arguments you can give ps to get process information:
- ps
- By itself it will list all of the processes active in your current shell
- ps -u <user>
- Will show all processes run by the specified user
The second command you can use to get process information is the top command. Top displays information about the top CPU processes. You run by top by just typing top in your shell. In it you will see a display with a lot of information. The first four lines contains miscellaneous information about the system, such as the load averages, cpu usage, total number of processes and memory usage. Below that you will see the top processes default sorted by CPU usage in descending order. It should be noted that the process that show up are all the processes in the system and not just the ones run by yourself.
[edit] Killing Processes
Killing processes should first be done in the active window. Killing without using the application's quitting function can result in settings and current work not getting saved (even if you don't realize it should be getting saved). Killing via the instructions on this page reccommended only if the process is hung and there is no way to quit out of it without forcing it to quit.
Killing a process is done with the kill command. kill is an easy command to use, all you need to type most of the time is kill <PID>, where PID is the process ID (gotten by following the process listing directions in the section above). This works well on programs that are internally hung (stuck in an endless loop or a very long calculation) however sometimes the program may crash and hang and thus the default kill command may not successfully kill the window. In this case you can type kill -9 <PID>. The -9 will force the application to exit wheither it wants to or not.
Warning:You should try the default kill command first (without the -9) because some applications have code to clean up when they see that it is being killed, while using the -9 immediately exits. The only exception is if you are sure you want the application to exit without cleaning up.
