Skip to main content

2025-01-16

Quote

“I once might have been a good person, but now, I’m just a soldier.” — Overwatch · smallxu


How to Move a Running Command to the Background

In Linux, if you’ve already started a command in the foreground and want to continue it in the background, you can do so without restarting it. The basic workflow is: (1) suspend the process with Ctrl + Z, (2) resume it with bg, (3) detach it from your terminal with disown, and (4) verify its status with jobs. This ensures your command keeps running even after you close the shell.


1. Suspend the Current Process

Press:

Ctrl + Z

This stops (pauses) the running command and returns you to the shell prompt:

[1]+  Stopped   your-command

2. Resume in the Background

Enter:

bg

This will restart the most recently suspended job in the background:

[1]+ your-command &

3. Prevent Termination on Terminal Close

By default, background jobs receive a SIGHUP (hangup) signal when the terminal closes and may be terminated. To avoid this, run:

disown

This removes the job from the shell’s job table so it won’t be sent a hangup signal upon exit.


4. Check Background Jobs

To list all jobs and their states, use:

jobs

You’ll see output like:

[1]+  Running   your-command &

With these steps, your previously foreground command is now running in the background and will persist even if you close the terminal.