Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get bg jobs to fg after closing terminal window/session

TL;DR How can I get a bg (background) job into fg (foreground) after I close the terminal window?

FULL I'm using terminal to start Spotify and put it into background with CTRL+Z and bg. Everything works as expected within this window: fg puts the process again in forground and jobs shows me Spotify as a running process. The problem occurs if I close the terminal window.

After I close the terminal window and open a new terminal window, the jobs command can't find Spotify running in the background. Therefore I can't access the process for stopping or closing anymore.

What is the advantage of this behaviour CTRL+Z, fg, bg & jobs ? And how can I get back my process?

like image 791
ucyo Avatar asked Sep 02 '25 04:09

ucyo


2 Answers

You can search your old process via ps -A. Or ps -A | grep <application name>. If you found it, then use reptyr <pid> to get the application on your new console. But if you closed the old terminal, all the terminal applications will probably be closed also, so it's too late.

Use screen, before you start your app. This will help you to reconnect to your session from another terminal.

like image 117
rundekugel Avatar answered Sep 04 '25 23:09

rundekugel


The job numbers only refer to background processes running under your current shell. The shell couldn't know the job numbers associated with the old shell.

like image 41
sweden Avatar answered Sep 05 '25 00:09

sweden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!