Suspend and resume shell processes
Andreas Bergström
Posted on April 11, 2023
In a shell environment, there may be times when you need to pause a process or move it to the background to perform other tasks. This is where the control key combination Ctrl+Z and the commands fg and bg come in handy.
Ctrl+Z is used to suspend a running process. When you press Ctrl+Z, the process is paused, and you are returned to the command prompt. The paused process is still running, but it's not doing anything, and it's waiting for further instructions.
To resume a paused process, you use the fg command. The fg command is used to bring a paused process back to the foreground. When you use the fg command, the paused process resumes, and you can interact with it again.
Sometimes you may want to move a process to the background so that it continues running, but you can still interact with the shell. You can do this by using the bg command. The bg command is used to move a paused process to the background.
Here's an example of how to use Ctrl+Z, fg, and bg:
- Start a long-running process in the shell, such as a file copy operation.
$ cp bigfile.txt /mnt/external/
- While the process is running, press Ctrl+Z to pause it.
^Z
[1]+ Stopped cp bigfile.txt /mnt/external/
- Use the jobs command to see the list of paused processes.
$ jobs
[1]+ Stopped cp bigfile.txt /mnt/external/
Resume the paused process using the fg command.
$ fg
cp bigfile.txt /mnt/external/
While the process is running again, press Ctrl+Z to pause it.
^Z
[1]+ Stopped cp bigfile.txt /mnt/external/
Move the paused process to the background using the bg command.
$ bg
[1]+ cp bigfile.txt /mnt/external/ &
Use the jobs command to see the list of running processes in the background.
$ jobs
[1]+ Running cp bigfile.txt /mnt/external/ &
Now you can continue using the shell while the process continues to run in the background. If you need to interact with the process again, you can bring it back to the foreground using the fg command.
In conclusion, the Ctrl+Z key combination and the fg and bg commands are useful tools for managing processes in a shell environment. They allow you to pause, resume, and move processes to the background, making it easier to multitask and manage your system efficiently.
Posted on April 11, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024