Learn by reading through in order

Checking and Stopping Processes — ps / kill

Learn to list running processes with ps, check your own PID with $$, and stop a process you sent to the background with & using kill — hands-on in a browser terminal.

Viewing Running Processes — ps

A process is each individual running program.

ps is the command that lists the processes running right now.

Each process is assigned a unique number called a PID (process ID), which serves as the marker when you stop it later.

When the list is long, pipe it to grep, like ps | grep sleep, to narrow processes down by name.

What a process is — a running instance of a program
programshprogramsleepprogramvishprocesssleepprocessviprocessPID 101PID 142PID 188
Running a program makes it a 'process', and each one is assigned a unique PID. ps lists these running processes.
CommandMeaning
psList running processes with PIDs
ps | grep nameNarrow processes down by name
echo $$Show the current shell's own PID
cmd &Run a command in the background

You can pull out the shell's own PID with the special variable $$.

$$ is a special variable that the shell fills with its own PID.

Running echo $$ shows the number of the shell you're typing in, and you'll find the same number in the ps list.

Reading the current process with ps and $$
pslist running processes with PIDsecho $$show the current shell's PID
ps lists the PIDs of running processes; echo $$ shows the current shell's PID.
ps          # list of running processes (with PIDs)
echo $$     # this shell's own PID
ps -ef      # a more detailed list (format varies by environment)

① Run ps to show the list of currently running processes.

② Run echo $$ to show the PID (number) of the very shell you're typing in.

③ Run ps again and visually confirm the number from step ② appears in the list. (Run it correctly and an explanation will appear.)

Linux console
0 / 2 completed
Loading Linux Terminal...

Background Execution and Stopping — & and kill

Adding & to the end of a command runs it in the background, letting you move on to the next operation without waiting for it to finish.

sleep 30 & runs a process that just waits, doing nothing for 30 seconds, in the background, which makes it good for practicing how to stop a process.

To stop a background process, pass its PID to kill.

The shell shows the PID right after you start it with &, and you can also check it in the ps list.

Passing the PID to kill sends a termination signal to that process.

For a process that won't stop with a normal kill PID, send a force-quit signal with kill -9 PID.

CommandSignal sentUse
kill PIDSIGTERM (request to quit)Normal stop; lets the process clean up
kill -9 PIDSIGKILL (force quit)Forcibly drop a process that won't stop
From background execution to stopping
sleep 30 &runs in background; PID is shownpsfind sleep's PID in the listkill PIDend the process with that PID
Send it to the background with &, find the PID with ps, and end that process with kill.

Give kill the right PID

kill sends a termination signal to the process with the PID you pass.

Getting the number wrong would stop a different process, so check the target PID in the ps list before running it.

About kill in this environment

In this browser environment, passing a PID to kill may not stop the process the way it would on a real system, and you may see No such process.

In this course you learn the form of the commands kill PID / kill -9 PID.

On a real server, the flow of checking the PID with ps and stopping it with kill works exactly as shown.

sleep 60 &     # run a process that waits 60 seconds in the background
ps             # find sleep's PID in the list
kill 123       # pass the PID you found (e.g. 123) to end it

① Run sleep 30 & to start a process that just waits 30 seconds in the background.

② Run ps | grep sleep to narrow the list to just the sleep line and find its PID (number).

③ Pass the number you found in step ② to kill to end that sleep process.

Linux console
0 / 3 completed
Loading Linux Terminal...
QUIZ

Knowledge Check

Answer each question one by one.

Q1What does ps show when you run it?

Q2What does echo $$ show?

Q3What happens when you add & to the end of a command?