Suspend and Resume Processes in Linux

Russell Jones - Aug 27 - - Dev Community

Image description

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

  1. Start a process:
 $ sleep 100

Enter fullscreen mode Exit fullscreen mode

This command starts a process that will sleep for 100 seconds.

  1. Suspend the process:
 Ctrl+Z

Enter fullscreen mode Exit fullscreen mode

This suspends the sleep process and puts it in the background.

  1. Check jobs:
 $ jobs
 [1]+ Stopped sleep 100

Enter fullscreen mode Exit fullscreen mode

This lists the jobs, showing that the sleep process is stopped.

  1. Resume in background:
 $ bg %1
 [1]+ sleep 100 &

Enter fullscreen mode Exit fullscreen mode

This resumes the sleep process in the background.

  1. Bring to foreground:
 $ fg %1
 sleep 100

Enter fullscreen mode Exit fullscreen mode

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.
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player