Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

5 Answers 5

up vote 7 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

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 at 0:49

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

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.