1

I have a long running python script that keeps getting interrupted by battery issues or my own stupidity. I can use (CTRL-Z) to stop the process and then fg to restart it, but as far as I can tell that will only work if I am in the same session and it hasn't been logged out.

Is there a way to deep stop the process so I can restart it at a later time after a logout without having to start from the beginning?

1

Sure, you can use screen, a terminal multiplexer, to solve this problem:

  1. Install screen from terminal: sudo apt-get install screen
  2. Start screen with command screen
  3. You will see an informative message, press Space until you see a terminal prompt
  4. Start your python script
  5. Detach from the screen instance by pressing Ctrl+A and the D button.
  6. You will see a message on your previous prompt: [detached from 19836.pts-3.ubuntu]
  7. You are successfully detached, you can log out or do whatever you want (unless you power down the machine or kill the process).

When you want to re-attach to your previous screen session, just type screen -r in a terminal. You will see your python script that is still working.

You con find further documentation on the Ubuntu Wiki - Screen

Not the answer you're looking for? Browse other questions tagged or ask your own question.