27

Possible Duplicate:
How can I disown a running process and associate it to a new screen shell?

I launched a command that lasts a long time. I had to disconnect so I moved it in the background (with CTRL+Z and bg) before exiting.

Something like this:

$ my_command
***command is beeing executed and is taking a long time***
^Z
[1]+  Stopped                 my_command
$ bg
[1]+ my_command &
$ exit

I reconnected and can see the command in the process list but cannot recover with fg.

$ fg
-bash: fg: current: no such job

How do I recover my job in the foreground?

SamK
  • 1,610
  • Hm, the question linked from the possible-duplicate banner is arguably of lesser quality (it's inconsistent, i.e. it talks about pausing but also keeping it running in the background) and addresses a different situation, i.e. there OP hasn't already disconnected from the shell (an it's even unclear if the ssh connection needs to be terminated). – maxschlepzig Dec 21 '23 at 11:37

1 Answers1

32

If you've already started something somewhere, backgrounded it, and now need to attach it to a new terminal, you can use reptyr to re-attach it. (The man page summarises the command as "Reparent a running program to a new terminal".)

The reason you can't see it in the "jobs" command or use "fg" to bring it to the foreground is because these commands are actually built-in to the shell. They don't technically detach the processes from the terminal you're connected with, so when you exit the shell they should exit as well (you should get a warning when you attempt to exit).

IpsRich
  • 767
criswell
  • 691
  • +1. thanks, i didn't know about repytr. useful....doing what it does manually always seemed too much work to be worth the effort most of the time. – cas Sep 25 '12 at 22:53
  • works so great... !!! thanks!!! sometimes you need to use 'sudo' to run the command. syntax: "sudo reptyr –s PID " and it brings my python process from other terminal active in the current one. – ihightower Dec 13 '14 at 11:08
  • terminal closed unexpedtly while running wget. wget continued to provide log and new files. reptyr failed to bring it to terminal again: sudo reptyr -s 783668 [-] Timed out waiting for child stop. [-] Unable to open the tty in the child. Unable to attach to pid 783668: Permission denied – Estatistics Aug 01 '22 at 14:08
  • reptyr -L PID solved the above problem :) – Estatistics Aug 01 '22 at 14:18