This page covers commands for managing processes, sending signals, and handling background/foreground jobs.
36. kill
- Send Signals to Processes
Purpose: The kill
command is used to send signals to processes. The most common use is to terminate processes (kill them).
Syntax: kill [SIGNAL] PID...
Common Signals:
-9
orSIGKILL
: Forceful termination. Cannot be caught or ignored by the process. Use as a last resort.-15
orSIGTERM
: Graceful termination. Asks the process to shut down. The process can perform cleanup before exiting. This is the default signal if none is specified.
You can find the PID (Process ID) using commands like ps aux
or top
.
Examples:
kill 12345 # Send SIGTERM (15) to PID 12345 kill -9 54321 # Forcefully kill PID 54321
37. killall
- Kill Processes by Name
Purpose: The killall
command terminates processes by their name, rather than by their process ID (PID). This is useful for stopping all instances of a specific application.
Syntax: killall [OPTIONS] PROCESS_NAME...
Key Options:
-SIGNAL
: Specify the signal to send (e.g.,-9
for SIGKILL).
Examples:
killall firefox killall -9 nginx
Caution: Ensure you know what processes you are killing, as this affects all instances with that name.
38. bg
- Send a Job to the Background
Purpose: The bg
command resumes a suspended (stopped) job in the background. It allows the job to continue executing without tying up your terminal.
To suspend a running foreground job, press Ctrl+Z
.
Syntax: bg [JOB_SPEC]
Examples:
# Run a long command, then press Ctrl+Z # You'll see: [1]+ Stopped long_running_script.sh bg
This will resume the job in the background.
39. fg
- Bring a Job to the Foreground
Purpose: The fg
command brings a job that is running in the background (or suspended) back to the foreground, allowing you to interact with it directly in your terminal.
Syntax: fg [JOB_SPEC]
Examples:
# After running `bg` on a job, bring it back: fg # If you have multiple background jobs, use 'jobs' to see their numbers # then 'fg %JOB_NUMBER' (e.g., fg %2)
40. jobs
- List Active Jobs
Purpose: The jobs
command displays a list of currently active jobs (processes started from the current shell) that are either running in the background or suspended.
Syntax: jobs [OPTIONS]
Examples:
jobs
Output typically shows the job number, status (Running/Stopped), and the command that was executed.