Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've written quite a few shell scripts over the years (but I'm certainly not a sysadmin) and there's something that always caused me troubles: how can I fork a shell command immune to hangups in the background from a Bash script?

For example if I have this:

command_which_takes_time input > output

How can I "nohup" and fork this?

The following doesn't seem to do what I want:

nohup command_which_takes_time input > output &

What is the syntax I am looking for and what am I not understanding?

share|improve this question
up vote 9 down vote accepted

Try creating subshell with (...) :

( command_which_takes_time input > output ) &


~$ ( (sleep 10; date) > /tmp/q ) &
[1] 19521
~$ cat /tmp/q # ENTER
~$ cat /tmp/q # ENTER
(...) #AFTER 10 seconds
~$ cat /tmp/q #ENTER
Wed Jan 11 01:35:55 CET 2012
[1]+  Done                    ( ( sleep 10; date ) > /tmp/q )
share|improve this answer

You should try setsid(1). Use it like you'd use nohup:

setsid command_which_takes_time input > output

This (as per the setsid(2) manpage), does a fork(2), an _exit(2) of the parent process, then the child process calls setsid(2) to create a new process group (session).

You can't kill that by logging out, and it's not part of the Bash job control shebang. For all intents and purposes, it's a proper daemon.

share|improve this answer


/usr/sbin/daemon -f <command> <command args>
share|improve this answer

there is disown bash builtin command:

root@ntb1:~# sleep 120 & [1] 9180 root@ntb1:~# jobs [1]+ Running sleep 120 & root@ntb1:~# disown root@ntb1:~# jobs root@ntb1:~# ps aux | grep sleep | grep -v grep root 9180 0.0 0.0 4224 284 pts/0 S 17:55 0:00 sleep 120 root@ntb1:~# After disown, the job is disowned from your shell (so You can even logout) and it still remains running until finished.

See the 1st jobs command listed the sleep however the 2nd jobs after the disown did not. But using the ps we can see that the job is still running.

share|improve this answer

This will work (do not type any extra spaces):

command &>output.file
share|improve this answer
This seems to have nothing to do with the question, because it doesn't fork or achieve the equivalent of nohup. – Dan Getz Oct 6 '15 at 0:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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