198

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?

4
  • 1
    The nohup(1) idea is better than disown IMHO because disown is a shell-specific built-in of BASH while nohup is part of coreutils and likely to be everywhere. Commented Jun 5, 2009 at 6:23
  • Use batch, e.g. echo myprogram its arguments | batch Commented Jan 2, 2016 at 11:18
  • Here's how you can do it after it has started. You'll need to install reptyr and screen. Start a screen session by typing screen. find the pid of your program: ps aux| grep <myprogramname> . Inside your screen session, type reptyr <myprogrampid> you should see your program running in your screen session now. You can kill your ssh session, screen will remain running waiting for you to reattach. To reattach, run screen -ls to find a list of available sessions and reattach using screen -r <mysessionid>
    – Rolf
    Commented Sep 29, 2023 at 19:08
  • I've documented a safe way of doing it here, which answers the question: rolfen.github.io/notes/entries/linux/process-detach.html
    – Rolf
    Commented Sep 29, 2023 at 19:48

6 Answers 6

407

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..

11
  • 7
    Can you explain to me what exactly happens after each step?
    – omg
    Commented Jun 5, 2009 at 7: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... Commented Jun 5, 2009 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). Commented Jun 5, 2009 at 10:35
  • 17
    i test it, and doesn't work.. exit when i'm logout... Commented Jul 6, 2011 at 23:08
  • 4
    @ButtleButkus: You should be able to see them with ps x Commented Oct 17, 2012 at 10:43
105

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

nohup sleep 3600 &
6
  • 16
    Is there a way to bring this job back to foreground on logging in again?
    – Lord Loh.
    Commented Mar 12, 2012 at 21:10
  • Actually it redirects the output to nohup.out file in the same directory by default.
    – Gowtham
    Commented Dec 28, 2015 at 8:52
  • 1
    @LordLoh. Afaik, the only practical way to do this is to use tmux or screen. Commented Feb 11, 2016 at 21:20
  • 3
    this worked much better, thanks! I just say nohup <my program> 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
    Commented May 3, 2016 at 7: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
    Commented Apr 30, 2018 at 19:32
44

I would try the program screen.

6
  • 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
    Commented Jun 5, 2009 at 5: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
    Commented Jun 5, 2009 at 6:45
  • 10
    alternative to screen would be tmux
    – rubo77
    Commented Oct 8, 2012 at 13:13
  • 8
    This is the poster child for why link-only answers should never be used.
    – mbroshi
    Commented Jun 23, 2014 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 web.archive.org/web/20090106170543/http://www.rackaid.com/…)?
    – josliber
    Commented Dec 21, 2015 at 5:16
19

Start in the background:

./long_running_process options &

And disown the job before you log out:

disown
6
  • but the programme has already started to run..
    – omg
    Commented Jun 5, 2009 at 5:21
  • And will it contine to run as root?
    – omg
    Commented Jun 5, 2009 at 5: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
    Commented Jun 5, 2009 at 5: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
    Commented Jun 5, 2009 at 5:51
  • 2
    "disown" is just for abandoning control of a job it does not change the privileges of the process.
    – diciu
    Commented Jun 5, 2009 at 5:52
14

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

1
3

You could use screen, detach and reattach

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