How to recover a backgrounded job from a previous shell?

45,073

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

Share:
45,073

Related videos on Youtube

SamK
Author by

SamK

Updated on September 18, 2022

Comments

  • SamK
    SamK over 1 year

    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?

  • Alessio
    Alessio over 11 years
    +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.
  • ihightower
    ihightower over 9 years
    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.