Theory
Programs that are running
When a program is actually running on the server, it is called a process. Your shell is a process; the command you just launched is a process; the web server humming in the background is a process. The kernel gives each one a unique Process ID (PID), a number used to refer to it.
You met processes as a concept in Operating Systems. Here you learn to control them hands-on: see what is running, launch long jobs without freezing your terminal, and stop a process that has misbehaved. This lesson covers that toolkit.
Theory
Foreground and background
By default a command runs in the foreground: it takes over your terminal, and you wait, unable to type, until it finishes. Fine for quick commands, but painful for a job that runs for minutes.
Append an ampersand `&` and the command runs in the background instead: it starts, the shell prints its PID, and your prompt returns at once so you can keep working. For example, sleep 60 & launches a 60-second timer in the background and hands you back the prompt immediately. You can then check on such jobs with jobs, and stop one with kill.
At a glance
| Command | What it does |
|---|---|
| ps | List running processes (with their PIDs) |
| command & | Run command in the background; the prompt returns immediately |
| jobs | List the background jobs in this shell |
| Ctrl+Z | Suspend (pause) the current foreground job |
| bg / fg | Resume a job in the background / bring it to the foreground |
| kill PID | Send a signal to stop the process with that PID |
| sleep N | Pause for N seconds (handy in scripts) |
Practical
Launching and stopping a background job (verified)
$ sleep 60 & # start a 60-second process in the background
[1] 4231 # shell reports job number [1] and PID 4231
$ ps # see it running
PID TTY TIME CMD
4231 pts/0 00:00:00 sleep
$ kill 4231 # politely ask it to stop
$ jobs
[1]+ Terminated sleep 60Formula
Foreground blocks, background frees
The core distinction: a foreground job holds your terminal until it ends; a background job (started with &) lets you keep working while it runs.
If you started a long job in the foreground by mistake, press Ctrl+Z to suspend it, then type bg to let it continue in the background, freeing your prompt. Later, fg brings it back to the foreground. This foreground/background dance is how you juggle several tasks from one terminal without opening new windows.
Quiz
You run a long command and append & , as in ./backup.sh & . What happens?
- The command is deleted before running
- The command runs in the background and your shell prompt returns immediately, so you can keep working
- The command runs in the foreground and blocks the terminal until it finishes
- The & sorts the output alphabetically
Show the answer
The command runs in the background and your shell prompt returns immediately, so you can keep working
Appending & runs the command in the BACKGROUND: it starts, the shell prints its job number and PID, and your prompt returns right away so you can run other commands while it works. Option A is wrong: & does not delete anything; the command runs normally, just in the background. Option C describes running WITHOUT the &, that is the foreground behaviour that ties up the terminal, which is exactly what & avoids. Option D is invented; & has nothing to do with sorting. Use & for long jobs (backups, downloads) so they run while you carry on; check them with jobs and stop them with kill if needed.
Think first
What is the difference between kill and kill -9?
kill PID usually stops a process, but sometimes people use kill -9. What is the difference, and why not always use -9? Then tap.
Show the answer
kill sends a SIGNAL to a process, and the difference is how forceful that signal is. Plain kill PID sends the default TERM signal (number 15), which POLITELY asks the process to shut down. This is the right first choice because it lets the program clean up: save its files, finish writing to the database, release resources, and exit gracefully. Most processes obey it. But occasionally a process is stuck or ignoring TERM, and then kill -9 PID sends the KILL signal (number 9), which forces the kernel to terminate the process IMMEDIATELY, with no chance to clean up. That force is exactly why you should NOT reach for -9 first: because the process gets no opportunity to save or tidy up, you risk half-written files or corrupted data. The good habit is: try plain kill first (polite), give it a moment, and only escalate to kill -9 if the process truly refuses to die. Ask nicely before you force: TERM first, KILL only as a last resort.
Summary
Key takeaways
- A process is a running program; the kernel gives each one a unique Process ID (PID).
- ps lists running processes and their PIDs; jobs lists this shell's background jobs.
- A foreground command ties up the terminal until it finishes; appending & runs it in the background and returns the prompt at once.
- Ctrl+Z suspends a foreground job; bg resumes it in the background; fg brings a job to the foreground.
- kill PID stops a process by sending it a signal.
- Plain kill sends the polite TERM (15) so the process can clean up; kill -9 sends KILL, forcing immediate termination (last resort).
- Memory hook: & to background, ps to see, kill to stop; TERM first, -9 only if stuck.