Suspend and Resume Processes in Linux
Managing processes in Linux is an essential skill for any user. This post covers how to suspend, resume, and manage jobs using simple commands.
Suspending a Process
- Ctrl+Z: This command suspends the current foreground process, stopping its execution and putting it in the background. This is useful if you need to temporarily halt a process to free up the terminal for other tasks without terminating the process.
Background and Foreground Jobs
- jobs: This command lists all the jobs that are currently running or suspended in the background. It helps you keep track of the processes you have suspended or are running in the background.
- bg %n: This command resumes the suspended job
n
in the background, allowing it to continue running while you can still use the terminal. This is useful if you want a process to continue running without occupying the terminal. - fg %n: This command brings the job
n
to the foreground, making it the active process in the terminal. This is useful when you need to interact with a process that was running in the background.
Example
- Start a process:
$ sleep 100
This command starts a process that will sleep for 100 seconds.
- Suspend the process:
Ctrl+Z
This suspends the
sleep
process and puts it in the background. - Check jobs:
$ jobs [1]+ Stopped sleep 100
This lists the jobs, showing that the
sleep
process is stopped. - Resume in background:
$ bg %1 [1]+ sleep 100 &
This resumes the
sleep
process in the background. - Bring to foreground:
$ fg %1 sleep 100
This brings the
sleep
process back to the foreground.
Summary
- Ctrl+Z: Suspend the current process.
- jobs: List all jobs.
- bg %n: Resume a job in the background.
- fg %n: Bring a job to the foreground.