285

I'm doing some test-runs of long-running data migration scripts, over SSH. Let's say I start running a script around 4 PM; now, 6 PM rolls around, and I'm cursing myself for not doing this all in screen.

Is there any way to "retroactively" nohup a process, or do I need to leave my computer online all night? If it's not possible to attach screen to/nohup a process I've already started, then why? Something to do with how parent/child proceses interact? (I won't accept a "no" answer that doesn't at least address the question of 'why' -- sorry ;) )

ojrac
  • 2,953

13 Answers13

224

If you're using Bash, you can run disown -h job

disown

disown [-ar] [-h] [jobspec ...]

Without options, each jobspec is removed from the table of active jobs. If the -h option is given, the job is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If jobspec is not present, and neither the -a nor -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs.

gharper
  • 5,535
91

Use reptyr

From the README:

reptyr - A tool for "re-ptying" programs.
-----------------------------------------

reptyr is a utility for taking an existing running program and
attaching it to a new terminal. Started a long-running process over
ssh, but have to leave and don't want to interrupt it? Just start a
screen, use reptyr to grab it, and then kill the ssh session and head
on home.

USAGE
-----

  reptyr PID

"reptyr PID" will grab the process with id PID and attach it to your
current terminal.

After attaching, the process will take input from and write output to
the new terminal, including ^C and ^Z. (Unfortunately, if you
background it, you will still have to run "bg" or "fg" in the old
terminal. This is likely impossible to fix in a reasonable way without
patching your shell.)

A few blog posts by its author:

23

To steal a process from one tty to your current tty, you may want to try this hack:

http://www.ucc.asn.au/~dagobah/things/grab.c

It needs some reformatting in order to compile to current Linux/glibc versions, but still works.

Juliano
  • 5,592
18

When a process starts, STDIN, STDOUT and STDERR are connected to something. Generally you can't change that once the command is started. In the case you're describing, that's probably a tty associated with the ssh session. nohup pretty much just does ...

command < /dev/null > nohup.out 2>&1

That is, sets STDIN to /dev/null, STDOUT to a file and STDERR to STDOUT. Screen does much more sophisticated things involving setting up ttys that direct to itself.

I don't know of any way to retroactively nohup or screenize a running process. If you cd to /proc/$pid/fd and see what 0, 1 and 2 point to.

You might have some luck with disown, but not if the process tries to do anything with STDIN, STDOUT or STDERR.

Jeff Atwood
  • 13,264
freiheit
  • 14,844
13

Cryopid is a further development from the author of grab.c that freezes a process to a file, which you then run (inside screen) to resume the process.

TRS-80
  • 2,579
12

I can only give you a simple "No" without the why for the screen part, I'd be interested in the reason myself thou.

However have you tried disown (a bash builtin)

~ $ echo $SHELL
/bin/bash
~ $ type disown
disown is a shell builtin
~ $ help disown
disown: disown [-h] [-ar] [jobspec ...]
     By default, removes each JOBSPEC argument from the table of active jobs.
    If the -h option is given, the job is not removed from the table, but is
    marked so that SIGHUP is not sent to the job if the shell receives a
    SIGHUP.  The -a option, when JOBSPEC is not supplied, means to remove all
    jobs from the job table; the -r option means to remove only running jobs.
serverhorror
  • 6,538
10

nohup on Solaris/OpenSolaris has a -p flag to nohup a running process - for instance, see the Solaris 10 nohup man page.

alanc
  • 1,500
5

I recently saw a link to neercs, which is a screen-like utility built using libcaca, a colour ascii-art library. Amongst other features, it boasts the ability to grab an existing process and re-parent it inside your neercs (screen) session.

I've not used it however, so I cannot comment on whether it works or not.

2

I'm probably under-thinking this so feel free to correct me (I already learned about disown!)... Wouldn't a ctrl-Z and "bg" work to at least get the process running in the background? Or is the key issue that you'd still want to see STDOUT while it runs?

Chris_K
  • 3,434
2

If you can live with not being able to interact with the process and you don't object to loading random kernel modules, you could do worse than to look at Snoop. Alternatively, there are a couple of other projects. Here is one call injcode, which can mostly do what you want to do.

David Pashley
  • 23,963
2

You can redirect process output to file with gdb

https://blog-en.openalfa.com/how-to-detach-from-the-terminal-a-running-process-in-linux

and then disown it

Upd: I had to run gdb with sudo and slightly different gdb command in Ubuntu 18.04:

p (int)dup2(open("/tmp/test.stdout", 1), 1)

d9k
  • 123
1

I wanted to use nohup (or similar) to start the links command-line browser and to attach to it after to download a file from ASP.NET website with complicated authentication process and a lot of hidden view state that made it difficult to use do the job with curl/wget.

I finally ended up using tmux which solved the job just great:

  1. Run tmux
  2. Run your app (links in my case) and leave it running
  3. Close SSH session, the app will remain running
  4. Connect with SSH to the same machine later and run tmux attach to bring the app back to screen
-1

Is it that you're worried about the session timing out? In that case you could Ctrl-z and bg the process, and then just put somthing to keep the session alive like a "ping -t localhost" or "top".

If it's that you want to logout, then I'm afraid I can't add to the other comments.