Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring an Orphaned Background Process back to Foreground?

Tags:

shell

process

ps

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

like image 475
Rudra Murthy Avatar asked Aug 13 '12 07:08

Rudra Murthy


People also ask

How do you bring a disowned process to the foreground?

Use Ctrl + Z to suspend a program then bg to run the process in background and disown to detach it from your current terminal session. With the built-in bash job call you can list all the existed backgrounded process, you can use fg to run the process in foreground again as long as it didn't get detached.

Which command is used to bring the background process to foreground?

Use fg cmd to bring a background process to foreground.

How can you bring a background job to the foreground in Linux?

Bringing a Background Job to the Foreground. We can reconnect a background job to our terminal with the Linux command fg. This command will bring job 2 into the foreground. If no job ID is given, fg will assume we're referring to the current (suspended) job.


2 Answers

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

like image 168
knightrider Avatar answered Oct 09 '22 03:10

knightrider


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.

like image 28
Stuart Marks Avatar answered Oct 09 '22 03:10

Stuart Marks