135

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

147

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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?
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.
@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.
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
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?
|
101

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 Comments

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?
This is the best answer. Most comprehensive.
Definitely the best answer, don't know why so little ups
@VladGanshin No.
|
59
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 Comments

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" :) )
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!
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.
What if I do this, but want to stop the background process?
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.
|
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 Comment

Thanks, thats a cool little trick... Starting to really appreciate some of the shell goodness...
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 Comments

@LadenkovVladislav I'm 95% certain you can install screen on RedHat. I have on CentOS.
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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.