176

Possible Duplicate:
Can I nohup/screen an already-started process?

On Unix (specifically, Linux), I've started a job in a regular ssh->bash session. I'd like to leave work soon, but I now realize that the job is going to take several hours.

If I had just started this job in screen, I could detach and go home. But I didn't.

Is there any way to disconnect the job from its ssh session, so I can shut down my computer, (dropping the TCP connection and killing the ssh session), and yet have the program keep running?

I don't care about its output -- in fact, I redirected stdout to a file. I just want it to run to completion.

0

9 Answers 9

240

You can press ctrl-z to interrupt the process and then run bg to make it run in the background.

You can show a numbered list all processes backgrounded in this manner with jobs.

Then you can run disown %1 (replace 1 with the process number output by jobs) to detach the process from the terminal.

In spite of the name, the process will still be owned by you after running disown, it will just be detached from the terminal you started it in.

This answer has more information

4
  • 2
    How safe is it to try this procedure? Any gotchas?
    – ndemou
    Jul 24, 2016 at 13:53
  • 1
    Any idea on how to also redirect stdout and stderr without restarting the process?
    – Gauthier
    May 21, 2019 at 11:56
  • 2
    jobs -l lists the process number required for disown - the process number is not included in the output on CentOS
    – smile2day
    Dec 12, 2021 at 22:09
  • One gotcha is that the process will immediately start dumping its output to your console as soon as it is backgrounded, and never stop. So you'll probably have to kill that session anyway. Jan 21, 2022 at 9:16
21

I believe the command you are looking for is "disown"

20

Use

nohup command &

Is beautiful:

root@index:~# nohup /usr/bin/curl localhost/begemot.php &
[1] 894
root@index:~# nohup: ignoring input and appending output to `nohup.out'

and exit of this terminal, a task proceed to be continued...

0
10

From within your bash shell:

control+z

Then, to continue running the job in background:

bg

Later, you can type jobs to see if your process is still running.

5
  • 3
    Won't it be killed when I close my shell?
    – mike
    Jul 1, 2009 at 22:24
  • 1
    No. Well, at least not if you're running bash. The job will detach from your ssh session's PPID, and still be running on the server as your username.
    – Joe
    Jul 1, 2009 at 23:20
  • 17
    The instructions above will cause the process to exit when you close the shell. You additionally need to run the 'disown' command/builtin (this is in bash), which will disassociate the process from the shell and not send the HUP signal to the process on exit.
    – Mark
    Jul 1, 2009 at 23:25
  • 2
    On my version of bash [3.2.25(1)-release (i686-redhat-linux-gnu)], I tested the process, and didn't have to disown the job before disconnecting. The child PID disconnected from the parent PID, and kept running as my username without interruption. YMMV, I suppose.
    – Joe
    Jul 1, 2009 at 23:38
  • @Joe by default SIGHUP is not sent to background jobs in bash (if I understand it correctly unix.stackexchange.com/a/608872)
    – Winand
    Mar 30, 2022 at 9:31
8

When using reptyr inside screen, you can move the process (or its output) into screen.

6

I have taken to setting up screen to auto-run when I connect to hosts I use regularly, to avoid this issue.

http://tlug.dnho.net/?q=node/239 is one way of doing it, though there are other variations out there.

Has saved me once or twice when I've had an unexpected disconnection mid-session and would otherwise have forgotten to start screen before I started something long-winded.

3
  • This is an excellent answer! However, I was wondering how to compare if [ $TERM = "screen" ] with a wildcard like so: screen* but * seems not to be working as a wildcard.
    – bomben
    Sep 25, 2019 at 19:51
  • Ah, got it, the * needs to be outside: "screen"*.
    – bomben
    Sep 25, 2019 at 19:55
  • 1
    @Ben - these days byobu is a common wrapper for screen or tmux included with most Linux distributions (either by default, or available in the standard repositories). It includes byobu-launcher-install which automates this sort of setup for you. See byobu.co and manpages.ubuntu.com/manpages/disco/en/man1/… for more info. Sep 26, 2019 at 14:16
5

Using crtl-Z then bg or using & when running the command is not reliable at 100%. It will work in most case but may not work as expected in some case (I/O usage, ...).
Screen is a reliable solution.

1
  • 3
    Note that retty (pasky.or.cz/~pasky/dev/retty) is a tool that can let you move a process running outside of screen into screen. It can be usefull to put you job in a screen if you forget. Then you can safely detach screen without any risk of the job to stop.
    – radius
    Jul 1, 2009 at 22:45
4

If you are using bash, you can suspect with control-Z and background with bg.

Job control (from man bash)

 If the operating system on which bash is running supports job 
 control, bash contains facilities to use it.   Typing the  
 suspend  character  (typically ^Z, Control-Z) while a process 
 is running causes that process to be stopped and returns control 
 to bash.  Typing the delayed suspend character (typically ^Y, 
 Control-Y) causes the process  to  be stopped  when  it  
 attempts to read input from the terminal, and control to be 
 returned to bash.

help bg

bg: bg [job_spec ...]
    Place each JOB_SPEC in the background, as if it had been started with
    `&'.  If JOB_SPEC is not present, the shell's notion of the current
    job is used.

help jobs

jobs: jobs [-lnprs] [jobspec ...] or jobs -x command [args]
    Lists the active jobs.  The -l option lists process id's in addition
    to the normal information; the -p option lists process id's only.
2
  • Won't it be killed when I turn off my computer, thus severing the SSH session and killing the shell?
    – mike
    Jul 1, 2009 at 22:25
  • Wait, are you running the command on your local computer and using SSH for some input/output? Or is this running on the remote computer? If the command is running on the remote computer, then you should be able to background it and disconnect.
    – Zoredache
    Jul 1, 2009 at 22:28
3

If you know beforehand that you want to have it running in the background, the unix command at is a good option. It starts commands in the background at scheduled time, just like a cron job that does not repeat. Depending on your distribution, you might need to install it and to start the atd daemon.

Not the answer you're looking for? Browse other questions tagged .