Linux and Command-Line Basics
While a long-running ./sim.sh is running in a terminal, you want to use other commands in the same terminal. Which of the following is what happens when you press Ctrl+Z and then run bg?
- ACtrl+Z ends sim.sh, and running bg starts the same command again from the beginning in the background
- BCtrl+Z suspends sim.sh, and bg resumes it from where it stopped, in the background
- CCtrl+Z moves sim.sh to the background where it keeps running, and running bg suspends it
Show answer and explanation
Answer: B. Ctrl+Z suspends sim.sh, and bg resumes it from where it stopped, in the background
Pressing Ctrl+Z sends SIGTSTP to the job running in the foreground, and the job is suspended. bg tells that job to resume and continue in the background, and fg brings it back to the foreground. The jobs and their states can be checked with jobs, and to run something in the background from the start, it is launched with &, as in "./sim.sh &". However, background jobs may also stop when the terminal connection is lost, so long-running work needs a separate measure.
- A. Ctrl+Z suspends the job rather than ending it. bg resumes it from where it stopped, not from the beginning.
- B. Correct. bg resumes the suspended job in the background.
- C. The roles of Ctrl+Z and bg are reversed. Ctrl+Z suspends, and bg resumes in the background.