Identifying Processes
In Linux, every running program or application is considered a process. Each process has a unique identifier called a Process ID (PID). Before you can terminate a process, you need to identify it using its PID.
Let's start by learning how to view running processes using the ps
command. This command displays information about active processes in your system.
First, open a terminal in the LabEx VM environment and run the following command:
ps
You will see a basic list of processes that are running in your current terminal session. The output should look similar to this:
PID TTY TIME CMD
2104 pts/0 00:00:00 zsh
2116 pts/0 00:00:00 ps
This output shows:
PID
: The Process ID number
TTY
: The terminal type the process is running on
TIME
: The amount of CPU time the process has used
CMD
: The command that started the process
To see a more comprehensive list of all processes running on the system, use the following command with additional options:
ps -aux
The options mean:
-a
: Show processes from all users
-u
: Display detailed user-oriented format
-x
: Include processes without a controlling terminal
The output will be much longer and more detailed, showing all processes running on the system:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 167936 11876 ? Ss 10:30 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S 10:30 0:00 [kthreadd]
...
labex 2104 0.0 0.1 11108 5456 pts/0 Ss 10:35 0:00 zsh
labex 2130 0.0 0.0 9828 3384 pts/0 R+ 10:36 0:00 ps -aux
Now, let's create a simple background process that we can use for practice:
sleep 300 &
sleep_pid=$!
This command starts a process that will "sleep" (do nothing) for 300 seconds (5 minutes). The &
symbol runs it in the background, and $!
captures the PID of the most recently started background process.
Let's verify the process is running:
echo "The sleep process ID is: $sleep_pid"
ps | grep sleep
You should see output similar to:
The sleep process ID is: 2135
2135 pts/0 00:00:00 sleep
Now you know how to identify processes and find their PIDs. In the next step, we'll learn how to terminate this process.