191

Possible Duplicate:
Prevent a background process from being stopped after closing SSH client

I have a program that takes a lot of time to finish. It is running as root over ssh.
I want it to continue to run after I logout,is this possible and how would I achieve this?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
omg
  • 136,412
  • 142
  • 288
  • 348

6 Answers6

401

Assuming that you have a program running in the foreground, press ctrl-Z, then:

[1]+  Stopped                 myprogram
$ disown -h %1
$ bg 1
[1]+ myprogram &
$ logout

If there is only one job, then you don't need to specify the job number. Just use disown -h and bg.

Explanation of the above steps:

You press ctrl-Z. The system suspends the running program, displays a job number and a "Stopped" message and returns you to a bash prompt.

You type the disown -h %1 command (here, I've used a 1, but you'd use the job number that was displayed in the Stopped message) which marks the job so it ignores the SIGHUP signal (it will not be stopped by logging out).

Next, type the bg command using the same job number; this resumes the running of the program in the background and a message is displayed confirming that.

You can now log out and it will continue running..

tokhi
  • 21,044
  • 23
  • 95
  • 105
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • 7
    Can you explain to me what exactly happens after each step? – omg Jun 05 '09 at 07:29
  • 14
    You press ctrl-Z. The system suspends the running program, displays a job number and a "Stopped" message and returns you to a bash prompt. You type the "disown -h %1" command (here, I've used a "1", but you'd use the job number that was displayed in the "Stopped" message) which marks the job so it ignores the SIGHUP signal (it will not be stopped by logging out). Next, type the "bg" command using the same job number. This resumes the running of the program in the background and a message is displayed confirming that. You can now log out and it will continue running... – Dennis Williamson Jun 05 '09 at 10:23
  • 4
    ...You should be aware that when you use the "bg" command the result is the same as if you'd run your program in the background with an ampersand (&). It won't have any output to stdout so it should be made to write output to a file (nohup will redirect standard output to nohup.out or ~/nohup.out if you don't redirect it yourself). – Dennis Williamson Jun 05 '09 at 10:35
  • 16
    i test it, and doesn't work.. exit when i'm logout... – Yuda Prawira Jul 06 '11 at 23:08
  • 1
    I tried disown on jobs already running in the background. It worked. But the jobs are no longer listed when running "jobs" command. So how do I find them now? – Buttle Butkus Oct 17 '12 at 06:07
  • 4
    @ButtleButkus: You should be able to see them with `ps x` – Dennis Williamson Oct 17 '12 at 10:43
  • Thanks for the hint @DennisWilliamson. I used `ps ax | grep tar` for example to see a `tar` command. Could also `grep` the `pid` if known. – Buttle Butkus Nov 30 '12 at 08:11
  • 1
    in my case it only works in this order: 1. start program 2. CTR-Z 3. bg 4. disown 5. verify with `jobs' – benjamin Jul 23 '13 at 20:49
  • 2
    This did not work for me. The programs never finished successfully after I logged out. – Makoto Oct 16 '13 at 00:35
  • 3
    This didn't work for me. Found out that when you log out, the output stream dies, and therefore so does your process, even if you `disown`. You can redirect, e.g. `foo &>/dev/null &; disown %1`, but be aware of the redirection buffer getting larger over time. There's solutions to this problem [here](http://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe). – James M. Lay Feb 11 '16 at 21:16
  • side-note: if you by any means need to switch the program to run in the "foreground" again, just use `fg` – Hulvej May 03 '16 at 07:08
97

You should try using nohup and running it in the background:

nohup sleep 3600 &
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 16
    Is there a way to bring this job back to foreground on logging in again? – Lord Loh. Mar 12 '12 at 21:10
  • Actually it redirects the output to `nohup.out` file in the same directory by default. – Gowtham Dec 28 '15 at 08:52
  • 1
    @LordLoh. Afaik, the only practical way to do this is to use `tmux` or `screen`. – James M. Lay Feb 11 '16 at 21:20
  • 3
    this worked much better, thanks! I just say `nohup ` and then `CTRL-Z` + `bg` Then I can logout. You can validate that it still runs by having another ssh looking at `top` and see the process keep going – Hulvej May 03 '16 at 07:26
  • You can also do `tail -f nohup.out` to see the output in real time, and then `kill` the PID if you need to restart. – veggiebenz Apr 30 '18 at 19:32
  • you should use tmux: https://www.hamvocke.com/blog/a-quick-and-easy-guide-to-tmux/, nohup didn't work for me with `julia myFile.jl` for whatever reason. Kept getting kill signals and freezes but tmux wored perfectly. – kilgoretrout Oct 25 '18 at 05:13
44

I would try the program screen.

Dominic Farolino
  • 1,362
  • 1
  • 20
  • 40
Janusz
  • 187,060
  • 113
  • 301
  • 369
  • 2
    While screen is a mighty nice tool, nohup is probably better suited for this task. Screen is only needed when you require the program to be interactive, and to be able to go back to the application at a later time. To be entirely honest, I often find myself using screen for the exact same reason as the question above. – wvdschel Jun 05 '09 at 05:59
  • 5
    Even with non-interactive task, it's nice to see that the program finished without errors. It's also good practice to always use screen in case of disconnection. – bbigras Jun 05 '09 at 06:45
  • 10
    alternative to screen would be `tmux` – rubo77 Oct 08 '12 at 13:13
  • 7
    This is the poster child for why link-only answers should never be used. – mbroshi Jun 23 '14 at 14:40
  • I know this is an old answer, but it has been flagged as a link-only answer and in fact the link is dead. Would it be possible to expand it out to show a brief example of how to use screen, perhaps from the originally linked example (still available at https://web.archive.org/web/20090106170543/http://www.rackaid.com/resources/linux-tutorials/general-tutorials/using-screen/)? – josliber Dec 21 '15 at 05:16
  • And link can't even be updated since the post is now too short :/ – Zitrax Feb 04 '16 at 13:10
19

Start in the background:

./long_running_process options &

And disown the job before you log out:

disown
diciu
  • 29,133
  • 4
  • 51
  • 68
  • but the programme has already started to run.. – omg Jun 05 '09 at 05:21
  • And will it contine to run as root? – omg Jun 05 '09 at 05:35
  • 1
    If the program is already running on the console and you can access the console it's running on hit "Ctrl+Z" to send it in the background and then disown the newly created job. As long as a process is started as root it will continue to run as root unless it drops privileges itself. – diciu Jun 05 '09 at 05:49
  • But the command is named 'disown',isn't that to say root will disown the program,and the program will not continue to run as root ? – omg Jun 05 '09 at 05:51
  • After stopping the process with "Ctrl+Z" you send it in the background with "bg" before disowning it. – diciu Jun 05 '09 at 05:52
  • 2
    "disown" is just for abandoning control of a job it does not change the privileges of the process. – diciu Jun 05 '09 at 05:52
14

You want nohup. See http://nixcraft.com/linux-software/313-ssh-nohup-connection.html

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 2
    Link is not working anymore, but I guess this one is more or less the same: https://www.cyberciti.biz/tips/nohup-execute-commands-after-you-exit-from-a-shell-prompt.html – chjortlund Mar 22 '18 at 08:31
3

You could use screen, detach and reattach

agf
  • 171,228
  • 44
  • 289
  • 238
brndnmg
  • 257
  • 6
  • 21