Linux Administration Bootcamp
Installing and Connecting to a Linux System
Intermediate Linux Skills
Linux Boot Process and System Logging
Disk Management in Linux
LVM - The Logical Volume Manager
User Management in Linux
Networking With Linux
Shell Scripting with Linux
Command Line Kung Fu
In this course, you will learn how to install a Linux system and connect to it, whether that be on Mac or Windows.
In this lesson, we'll be talking about how to list the running processes or running programs and commands. We'll talk about the difference between foreground and background processes and how to manipulate those. We'll also talk about how to kill processes.
To display the currently running processes, use the ps command. If no options are specified, ps displays the processes associated with your current session. To see every process, including ones that are not owned or running as you, use ps with a -e option. The -f option will give you more information in a full format listing. To see processes running as a specific user, use ps -u username. And to display more information about a specific process, use it's PID or it's process identification number. Ps space -p space PID.
Here are some common ps commands that you could use. Ps -e, to display all the processes, ps -ef, to give you a full listing of all the processes. So, a couple of different ways to display processes in a tree-like output. Ps space -e with a capital H, or -e --forest option. Again, to display a specific user's process, use ps space -u space that username.
There are other ways to view processes and running commands on a system. You can use the pstree command that displays processes in a tree format. You can use top, which is an interactive process viewer, or you can use htop. Htop is less common than top, so it may or may not be included in your Linux distribution.
To see a list of processes that are associated with your session, type ps. And you can see that I only have two processes, the batch process, which is my shell, and ps, which I ran to get the list of processes. If we want to look at a specific process, we can use -p, and supply a PID or a process ID. So, let's look at 1530. And that will only list that one specific process. To get a full listing, we can use a -f, and that will tell us some more information. We can do a -e for all the processes, and ef for a full listing. And let's pipe this to less. Then you can see the information that it's displaying. The user or UID, that the process is running as, the PID, the process ID, the PPID, which is the parent process ID. And you have some more information about what time it started and command name.
If you wanna get a full process listing of a specific user, you can use this command. We'll use me, or we could use, say, the root user. Let's look at this output in a tree format. That's another way to do that. Here's an example of the pstree command.
Let's look at the top command. And it places the processes that are using the most CPU resources at the top of the list. And type a question mark for help. I'll type Q to exit here. Can also look at this program called htop. It gives you the same information as top, but just in a slightly different format.
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 be displayed until that process exits. For long running programs, it can be convenient to send them to the background. Processes that are backgrounded still execute and perform their task. 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 control key and press the letter C. To suspend the process, type Control + Z. That will stop the process. A process that is suspended is not running in the background, it is actually stopped. To send a suspended process to the background, use the BG command. You can background a specific job number by proceeding it with a percent sign, for example, %3. If you don't supply a job number, 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. Like BG, FG will operate on the current job, unless you give it a specific job number. To kill a job, type the word kill, space, percent, and the job number. Kill requires that you give a job number or a process ID number. To list your jobs, type the jobs command. There are two ways to kill a running process. If the process is running in the foreground, simply type Control + C to kill it.
The other to kill a process is with the kill command. You can run kill by just supplying the PID or the process ID. You can also specify a signal to send to the process. The default signal used by kill is termination. You'll see this signal referred to as SIG term, or term for short. Signals have numbers that correspond to their names. The default term signal is number 15. So, running kill 123 is the same as running kill space -15.
Space 123 or kill space -TERM space 123. If a process does not terminate when you send it the term signal, use the kill signal, which is number nine. Let's start a program in the background. To do that, we'll end the command line with an ampersand. So, when a command is run in the background, two numbers are displayed. The number in brackets is the job number, and that can be referenced by proceeding it with the percent sign.
The second number is the PID. So, if we run ps -p, we'll give it the PID of 2373, we'll see that long running process command. Type jobs, you'll see that one is the job number for number one. You can also reference it by %1. We'll run the foreground command to bring this job to the foreground. And to kill a foreground process, we can type Control + C. And that has stopped our process.
So, let's start multiple jobs in the background. Type the jobs command here. The plus sign in the jobs output represents the current job, while the minus 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 job started in the background.
If no information is supplied to the FG or BG commands, then the current job is operated upon. The current job can be explicitly referred to by percent percent or percent plus. The previous job can be referred to by percent dash. For instance, we can run jobs. That one, this way. Or to look at the previous job, we can run this command.
To bring a job back to the foreground, we can type FG and the job number. Let's bring job number two to the foreground. And then from here, we can say kill it. And we'll just hit the... Let's see if that works. And then yes, now we have jobs number one, three, and four.
Let's bring job number one to the foreground. And we can suspend it by typing Control + Z. Type our jobs output, and we can see one is stopped. It's not in the background, it is actually stopped. It's not doing any work. If we wanna send it to the background, type BG, and then %1. Now, jobs one, three, and four, are running in the background.
Let's kill a job by job number. Let's kill job one. And then you'll notice that it's terminated. Let's just foreground job number three. And again, you don't have to supply FG. You can just simply use the job number. Let's take a look at the kill command real quick. Let's do a kill -l to list all the signals that we can send a process. Again, kill defaults to signal 15, which is SIG term. And a hard to kill process, you would want to use kill -9 or kill -kill for our process.
Let's start a couple of jobs here. Let's use kill 2394, 'cause that's the PID. See what happens. Yep, that terminated our job. We can also use... Let's do a kill -9 for 2396. And sure enough, that killed that job as well. To display information about running processes and programs, use the ps command.
To see all the processes running on a system with a full listing, use ps space -ef. To look at a process for a given user, use ps -u, followed by the account name. To kill a process that's in the foreground, type Control + C. To suspend a foreground process, type Control + Z.
To background that process, type BG. And to foreground a process, use the FG command. To display information about your running jobs, use the jobs command. You can use the kill command to kill jobs by job number with the percent sign, or you can kill programs and processes by PID.
QA: A world-leading tech and digital skills organisation
We help many of the world’s leading companies to build their tech and digital capabilities via our range of world class training courses, reskilling bootcamps, work-based learning programmes and Apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.