132

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...

2

5 Answers 5

145

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.

6
  • 1
    I 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 3, 2010 at 1:08
  • 4
    What is the difference between putting the & on the command line and putting it in the script? I was not aware that they were different. Jul 10, 2013 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, 2013 at 22:47
  • 1
    oh. 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 Jul 10, 2013 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? Oct 8, 2016 at 16:02
97

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 a SIGHUP, it also sends a SIGHUP 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).
  • nohup disconnects the process from the terminal, redirects its output to nohup.out and shields it from SIGHUP.

    • 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).
8
  • 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, 2016 at 23:52
  • disown command and on OSX
    – Ani Menon
    Jun 12, 2016 at 2:45
  • 9
    This is the best answer. Most comprehensive. Dec 27, 2016 at 14:35
  • 4
    Definitely the best answer, don't know why so little ups
    – HeberLZ
    Jan 26, 2017 at 3:55
  • 2
    @VladGanshin No.
    – Ani Menon
    May 10, 2020 at 7:15
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

6
  • 2
    Cool, 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 3, 2010 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, 2011 at 1: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. Nov 1, 2015 at 18:32
  • What if I do this, but want to stop the background process? Feb 2, 2016 at 20:36
  • 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, 2018 at 15:20
29

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)

1
  • 3
    Thanks, thats a cool little trick... Starting to really appreciate some of the shell goodness...
    – LorenVS
    Mar 3, 2010 at 21:37
8

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.

2
  • @LadenkovVladislav I'm 95% certain you can install screen on RedHat. I have on CentOS.
    – BuvinJ
    Oct 28, 2019 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, 2019 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.