I've got an init.d script which I want it to work in the background, even when I quit from the terminal. However I've tried making it run in the background with nohup but had "no hope", becasue when I quit, by looking at the pstree I can see the PID disappears and thus nohup stops working.

Q: Is there another apart from nohup way of pushing a script that works in the foreground to the background, even after you quit the terminal?


Ok, so there are several options:

  • disown

You can combine disown and & to push your script to the background

$ disown [your_script] &
[your_script] can be checked by the jobs command. Once typed you will see:

$ jobs
[1]+ Running [your_script]

And the killing can be done by kill %1, the 1 refers to the job number seen above. This is the better alternative to nohup as it does not leave the nohup.out files littered all over the file system.

  • screen

Is a "virtual" terminal which you can run from a "real" terminal (actually all terminals today are "virtual" but that is another topic for another day). Screen will keep running even if your ssh session gets disconnected. Any process which you start in a screen session will keep running with that screen session. When you reconnect to the server you can reconnect to the screen session and everything will be as if nothing happened, other than the time which passed.

Excellent source: Speaking UNIX: Stayin' alive with Screen".


I cannot perfectly understand your problem but...

In my case, I use screen for such long running background jobs. It provide virtual terminal, which is attachable/dettachable anytime. So I can logout from original session without closing the virtual session, and get that session back anytime I want.

anyway, this is not for init.d style scripts.


Use & after your command in script like :

your command here > /dev/null 2>&1 &
  • 1
    ,what does this command do? Could please elaborate on it!? – 3kstc Mar 25 '15 at 3:25
  • helio (:)) has explained it in his answer. – Faizan Akram Dar Mar 25 '15 at 12:57
  • Close. you need to use nohup or the command still exits. – Erik Knowles Jan 9 at 17:28

I have to run a long running command (rsync) via SSH (on a QNAP nas). That machine does not have nohup nor screen installed. I would like to be able to run the command without the need to keep my laptop with the ssh client running all the time. The answers suggested here with background or ampersand (&) simply do not work. People might not be aware that nohup actually shields the process for the SIGINT and SIGTERM interrupts. So without nohup I end up with the following error whenever I disconnect my ssh session:

rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(553) [receiver=3.0.7]rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(553) [generator=3.0.7]

So in my case setsid helped instead of nohup.

nohup your_command_here  > /dev/null 2>&1 &

You need to:

  1. Use nohup to prevent the process from being killed on exit
  2. Redirect stdout and stderror away from the terminal's output streams (otherwise your process's going down when the terminal's output streams get killed on terminal exit)
  3. Put the process in the background (that's the trailing ampersand)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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