I'm trying to use a shell script to start a command. I don't care if/when/how/why it finishes. I want the process to start and run, but I want to be able to get back to my shell immediately...
-
2Related, see [Difference between nohup, disown and &](http://unix.stackexchange.com/q/3886) on the Unix and Linux Stack Exchange. – jww Jun 11 '16 at 23:53
-
1For those looking for a more in-depth answer that talks about the differences between **`nohup`, `&` and `disown`**, [click here to scroll to the fourth answer](http://stackoverflow.com/a/37531889/1459669). – noɥʇʎԀʎzɐɹƆ Dec 27 '16 at 14:37
5 Answers
You can just run the script in the background:
$ myscript &
Note that this is different from putting the &
inside your script, which probably won't do what you want.

- 219,201
- 40
- 422
- 469
-
1I knew it was going to be something easy, thanks a ton... Linux just isn't my thing, but I'm trying to get up to speed... Btw, will this work when combined with nohup? – LorenVS Mar 03 '10 at 01:08
-
4What is the difference between putting the & on the command line and putting it in the script? I was not aware that they were different. – Jacob Sharf Jul 10 '13 at 22:41
-
11@JacobSharf, give it a try and you'll see. If the `&` is inside the script and you don't have a `wait`, the background command will be killed when the script exits. – Carl Norum Jul 10 '13 at 22:47
-
1oh. That explains a lot. I was recently noticing some effect that would be caused by that. Googling it actually lead me to this page. Thanks – Jacob Sharf Jul 10 '13 at 23:05
-
If `myscript` modifies the terminal environment, ex. it is a terminal initialization command that is not needed immediately and can be delayed, will it still modify the terminal environment? – noɥʇʎԀʎzɐɹƆ Oct 08 '16 at 16:02
-
The meaning of running in the background is, The default behavior of the kernel is Synchronous If we want to change it to Asynchronous then we use '&', Which makes the command run in an Asynchronous (parallel) way (that's why it looks to running in the background) – Abhijit Manepatil Jan 18 '23 at 11:15
Everyone just forgot disown
. So here is a summary:
&
puts the job in the background.- Makes it block on attempting to read input, and
- Makes the shell not wait for its completion.
disown
removes the process from the shell's job control, but it still leaves it connected to the terminal.- One of the results is that the shell won't send it a
SIGHUP
(If the shell receives aSIGHUP
, it also sends aSIGHUP
to the process, which normally causes the process to terminate). - And obviously, it can only be applied to background jobs(because you cannot enter it when a foreground job is running).
- One of the results is that the shell won't send it a
nohup
disconnects the process from the terminal, redirects its output tonohup.out
and shields it fromSIGHUP
.- The process won't receive any sent
SIGHUP
. - Its completely independent from job control and could in principle be used also for foreground jobs(although that's not very useful).
- Usually used with
&
(as a background job).
- The process won't receive any sent

- 27,209
- 16
- 105
- 126
-
I can't find `disown` on Debian or OS X. I thought it was a program, but I seem to be mistaken. What is it? – jww Jun 11 '16 at 23:52
-
[disown command](http://www.cyberciti.biz/faq/unix-linux-disown-command-examples-usage-syntax/) and on [OSX](http://ss64.com/osx/disown.html) – Ani Menon Jun 12 '16 at 02:45
-
9
-
4
-
There is also the widely available [`setsid`](http://man7.org/linux/man-pages/man1/setsid.1.html) to _immediately send the command in background_; the current session can be safely closed too. – bufh Jul 10 '19 at 15:00
-
Thanks. I still have doubt. Is it necessary to call `disown` if the command was run with `nohup` like `nohup my-script.sh > out 2>&1 &` – Vlad Ganshin May 08 '20 at 21:41
-
2
-
@jww `disown` is a [shell builtin command](http://manpages.ubuntu.com/manpages/impish/en/man1/bash.1.html#shell%20builtin%20commands) – Mr. Tao Aug 22 '21 at 17:58
nohup cmd
doesn't hangup when you close the terminal. output by default goes to nohup.out
You can combine this with backgrounding,
nohup cmd &
and get rid of the output,
nohup cmd > /dev/null 2>&1 &
you can also disown
a command. type cmd
, Ctrl-Z
, bg
, disown

- 55,454
- 12
- 93
- 132
-
2Cool, crazy how everything combines, I think the ordering would get to me at first, but I suppose you could just memorize it ( what you wrote or "nohup cmd & > /dev/null 2>&1" :) ) – LorenVS Mar 03 '10 at 21:38
-
I stumbled upon this tonight. I've been fighting with a shell script for 2 days and this suggestion got things working. Thank you mucho! – JD Long May 20 '11 at 01:43
-
Awesome, this is very useful. When running in background mode, you can check the command's output every once in a while using `tail nohup.out`, this will display the last 10 lines of the command output. I use this for rsync backup jobs to see what file it's currently at. – Martin Hansen Nov 01 '15 at 18:32
-
-
The shell prints a job identifier when you start a job precisely for this purpose. You can see your running jobs at any time with `jobs`. – tripleee Aug 13 '18 at 15:20
-
Discarding the output is a poor idea. Send it to a file so you can see what it's doing and react to any error messages. – tripleee Aug 13 '18 at 15:20
Alternatively, after you got the program running, you can hit Ctrl-Z which stops your program and then type
bg
which puts your last stopped program in the background. (Useful if your started something without '&' and still want it in the backgroung without restarting it)

- 15,828
- 8
- 54
- 65
-
3Thanks, thats a cool little trick... Starting to really appreciate some of the shell goodness... – LorenVS Mar 03 '10 at 21:37
screen -m -d $command$
starts the command in a detached session. You can use screen -r
to attach to the started session. It is a wonderful tool, extremely useful also for remote sessions. Read more at man screen
.

- 211
- 2
- 3
-
@LadenkovVladislav I'm 95% certain you can install screen on RedHat. I have on CentOS. – BuvinJ Oct 28 '19 at 12:44
-
On Ubuntu, I wanted to emulate the Windows / batch "start" command. I.e. asynchronously launch a foreground (gui) program, and continue on through a shell script. This does exactly that. – BuvinJ Oct 28 '19 at 12:46