Q1What does ps show when you run it?
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.
PID. ps lists these running processes.| Command | Meaning |
|---|---|
ps | List running processes with PIDs |
ps | grep name | Narrow 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.
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)
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.
| Command | Signal sent | Use |
|---|---|---|
kill PID | SIGTERM (request to quit) | Normal stop; lets the process clean up |
kill -9 PID | SIGKILL (force quit) | Forcibly drop a process that won't stop |
&, 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
Knowledge Check
Answer each question one by one.
Q2What does echo $$ show?
Q3What happens when you add & to the end of a command?