Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active October 3, 2023 09:59
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nepsilon/018cc29e8bbaeeb61965 to your computer and use it in GitHub Desktop.
Save nepsilon/018cc29e8bbaeeb61965 to your computer and use it in GitHub Desktop.
How to detach a process from the current terminal? — First published in fullweb.io issue #37

How to detach a process from the current terminal?

Closing the terminal will kill all processes launched from its shell instance that are still running. Here is how to detach a running process and run+detach a new process:

Scenario 1:

Let’s say we have ./long.sh running, and we want to detach it:

./long.sh

Press Ctrl+Z to pause the process:

[1] bash: suspended ./long.sh

Get its job ID with the jobs command:

jobs 
[1] + suspended ./long.sh

Then type bg, to run it in background:

bg %1 
[1] bash: continued ./long.sh

Use disown to detach it:

$ disown %1
# Using its job ID

Scenario 2:

You can also run+detach a process with:

nohup ./long.sh &
# stdout and stderr will be put in a file.
@GAZ082
Copy link

GAZ082 commented Jan 8, 2019

Good stuff! Thanks!

@kopax-polyconseil
Copy link

In Scenario 2, how do you get the pid ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment