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...
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.
7 Comments
& is inside the script and you don't have a wait, the background command will be killed when the script exits.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?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.
disownremoves 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 aSIGHUPto 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
nohupdisconnects the process from the terminal, redirects its output tonohup.outand 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
8 Comments
disown on Debian or OS X. I thought it was a program, but I seem to be mistaken. What is it?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
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.jobs.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
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.
nohup,&anddisown, click here to scroll to the fourth answer.