This tutorial talks about how to list the running processes or running programs and commands. The difference between foreground and background processes, how to manipulate those are also introduced. This tutorial also talks about how to kill processes.
Listing Processes and Information
To display the currently running processes use the
ps command. If no options are specified, ps displays the processes associated with your current session.
| ||Display the processes associated with your current session.|
To see every process including others that are not owned or run by you, use
-e option. The
-f option will give you more information and a full format listing. To see processes running as a specified user, use
ps -u username . And to display more information about a specific process, use
-p option and followed by it's PID (Process Idenfification number), e.g.
ps -p pid.
| ||Display every processes including others not owned by you.|
| ||Full format listing.|
| ||Display username's processes.|
| ||Display information for PID.|
Here are some common
ps commands that you could use.
| ||Display all processes.|
| ||Display all processes with full format.|
| ||Display a process tree.|
| ||Display a process tree.|
| ||Display user's processes.|
There are other ways to view processes and running commands on a system. You can use
pstree command, it displays processes in a tree format. You can use
htop to display the interactive processes view. htop is less common than top , so it may not be included in your Linux distribution.
| ||Display processes in a tree format|
| ||Interactive process viewer|
| ||Interactive process viewer|
To see a list of processes that are associated with your session, type
$ ps PID TTY TIME CMD 330 ttys000 0:00.00 bash 332 ttys000 0:00:00 ps
To look a specific process, use
-p and supply a PID:
$ ps -p 330 PID TTY TIME CMD 330 ttys000 0:00.00 bash
To get a full listing, use
$ ps -f UID PID PPID C STIME TTY TIME CMD robin 330 329 0 18:56 ttys000 0:00:04 bash robin 330 331 0 18:57 ttys000 0:00:04 ps
To display all processes, use
$ ps -e
-ef for a full listing.
$ ps -ef | less
To display a full process listing of a specific user, use
ps -fu username
$ ps -fu robin
To display root user's processes.
$ ps -fu root
To display processes in a tree format:
$ ps --forest $ ps -ef --forest $ pstree
To display the interactive process view, use
$ top $ htop
Background and foreground processes
Up until this point, all the commands you have been executing have been running in the foreground. When a command, process, or program is running in the foreground, the shell prompt will not bee displayed until that process exits.
For long-running programes, it can be convenient to send them to the background. Processes that are backgrounded still execute and perform their tasks. However, they do not block you from entering further commands at the shell prompt.
To start a process in the background, place an ampersand (
&) at the end of the command. To kill the current foreground process, hold down the Ctrl + c. To suspend the foreground process, type Ctrl + z. A suspended process is not running in the background. It is actually stopped.
| ||Start command in background.|
|Ctrl + c||Kill the foreground process.|
|Ctrl + z||Suspend the foreground process.|
To send a suspended process to the background, use
bg [%job_number] command. You can background a specific job number by proceeding it with a percent sign (
%) for example %12. If no job number is provided,
bg will operate on the current job. The current job is considered to be the last job that was stopped while it was
in the foreground, or the last job that was started in the background.
To foreground a background process, type
fg [%job_number]. Similarly, the
fg will operate on the current job unless you give it a specific job number.
To kill a job, type
kill %job_number. Kill requires that you give a job number or a process ID number.
To list your jobs, type the
| ||Background a suspended process.|
| ||Foreground a background process.|
| ||Kill a process by job number or PID|
| ||List jobs.|
Kill running process
If the process is running in the foreground, simply type Ctrl + c to kill it. The other way to kill a process is with the
kill command. You can run
kill PID to kill the process with PID. You can also specify a signal to send to the process,
kill [-signal] PID. The default signal used by kill is termination. You'll see this signal referred to as SIGTERM, or TERM for short. Signals have numbers that correspond to their names. The default TERM signal is number 15. So running
kill 12 is the same as running
kill -15 12 or
kill TERM 12.
If a process does not terminate when you send it the TERM signal, use the kill signal which is number 9,
kill -9 12.
ExamplesExample 1 - start a program in background
To start a program in background, simply end the command line with
$ ./a-program &  2425
When a command is run in background, two numbers are displayed. The 1st number (in square brackets) is the job number,
can be referenced by preceding it with a percent sign. The 2nd
number is PID, so we can run
ps -p 2425to see the process.
$ ps -p 2425 PID TTY TIME CMD 330 ttys000 0:00.00 a-programExample 2 - list jobs
jobs you'll see that 1 is the job number for number one
$ jobs + Running ./a-program &
You can also reference this by
$ jobs %1 + Running ./a-program &Example 3 - bring command to foreground and kill a foreground process
fg command to bring this command to foreground
$ fg ./a-program
And to kill the foreground process, simply type Ctrl + c.Example 4 - multi jobs
Suppose you have the 4 jobs running in the background, program-1, program-2, program-3, and program-4.
$ jobs  Running ./program-1 &  Running ./program-2 & - Running ./program-3 & + Running ./program-4 &
The + sign represents the current job, and the - sign represents the previous job. The current job is considered to be the last job that was stopped while it was in the foreground, or the last jobs started in the background.
If no information is supplied to the
bg commands, then the current job is operated upon. The current jobs can be explicitly referred to by
%+, the previous job can be referred by
$ jobs %% + Running ./program-4 & $ jobs %+ + Running ./program-4 & $ jobs %- - Running ./program-3 &Example 5 - kill and suspend foreground job
To bring job number 2 to the foreground:
$ fg %2 ./program-2
Then, to kill it by enter Ctrl + c, then we found job number 2 has gone.
$ jobs  Running ./program-1 & - Running ./program-3 & + Running ./program-4 &
Let's bring job number 1 to foreground
$ fg %1 ./program-1
Then, to suspend it by enter Ctrl + z, then we can see number one is stopped.
$ jobs + Stopped ./program-1 & - Running ./program-3 &  Running ./program-4 &
- Dependency injection
- Directives and Pipes
- Data binding
- HTTP Get vs. Post
- Node.js is everywhere
- MongoDB root user
- Prefer Async Script Loading
- Components, Bootstrap and DOM
- What is HEAD in git?
- Show the changes in Git.
- What is AngularJS 2?
- Confidence Interval for a Population Mean
- Accuracy vs. Precision
- Sampling Distribution
- Working with the Normal Distribution
- Standardized score - Z score
- Evaluating the Normal Distribution
- What is Nodejs? Advantages and disadvantage?
- How do I debug Nodejs applications?
- Sync directory search using fs.readdirSync