42

Well it goes like this, I had to run a program from my home by sshing into the server in my institution. I did not want my program to be terminated when the session closes(I didn't know about screen).

What i did was press Ctrl+Z and then type bg so that it executes in the background. The session got terminated. Now when I login from my institution machine and type ps -u username, it shows that the program is still running but I'm unable to bring it to foreground.

I tried fg and jobs but these commands don't give me any output.
Please someone help me..

Rudra Murthy
  • 710
  • 2
  • 8
  • 20

3 Answers3

36

If you have started the process without using "screen" command then you cannot take over that process. Basically you cannot take over a process that was started in a different shell.

When your session is terminated all the bg process will go the detached state. Though you might be able to see the details of such process you cannot fg them to a shell from login afterwards

knightrider
  • 2,063
  • 1
  • 16
  • 29
12

If a process has been orphaned, you can't "reparent" it to a different shell and use fg, bg, ^Z, ^C, and so forth to control it.

It seems you're asking implicitly how to control an orphaned process. Since you can see the process using the ps command, you have its pid. You can use this pid as the argument to the kill command, which will allow you to stop, continue, or terminate the process. You can't wait for the process to finish, but you can poll to see whether it still exists by using the kill -0 <pid> command.

Olivier
  • 303
  • 3
  • 14
Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
7

https://serverfault.com/questions/55880/moving-an-already-running-process-to-screen

Gives an alternative view on this question, The top answer suggests using Reptyr.

Community
  • 1
  • 1
chim
  • 8,407
  • 3
  • 52
  • 60
  • 2
    Short answer from the other posting: It is possible. Install `reptyr`, then run `reptyr [PID]` - but this may not work if one process (e.g., a bash-script) started another (sub-)process. – BurninLeo Sep 30 '15 at 21:26