2

I'm working on a problem on a remote server (I use ssh to connect). As I'm new to the domain, I'm using jupyter-notebook, so that I can examine and code. Some parts of my code take a long time to run(like maybe 4-5 hours) and I'm not able to maintain my connection that long. How can I keep my code running after the shell is closed? Should I just copy it into a .py file and use commands like tmux or screen?

petezurich
  • 9,280
  • 9
  • 43
  • 57
P.Alipoor
  • 178
  • 1
  • 2
  • 11
  • 3
    You have been asking a non-programming question and gave two possible solutions already. What do you expect as an answer? – Klaus D. Mar 31 '19 at 15:55
  • Possible duplicate of [Run python in terminal and don't terminate when terminal is closed](https://stackoverflow.com/questions/18614665/run-python-in-terminal-and-dont-terminate-it-when-terminal-is-closed). This question has been asked and answered so many times you have to make an effort to avoid finding an answer. – jww Apr 02 '19 at 21:08

2 Answers2

6

Use screen.

$ screen

This will create a screen session and when you are done, you can use the keys ctrl-a-d to detach.

To go back:

$ screen -list

This will show the list of detached screen sessions. To connect

$ screen -r

2

I use PM2, the Node.js process manager that works with Python scripts (and many more languages) as well.

Install and then start your code with:

pm2 start your_code.py

List your running processes with:

pm2 list 

And yes, you can start as many scripts from one single terminal as you like.

Have a look at the logs with:

pm2 logs

PM2 by default restarts your script after an error or termination. To prevent this and let your code only run once add the --no-autorestart flag:

pm2 start your_code.py --no-autorestart

The processes continue to run when you exit the terminal and are available when you log back into your server instance. You also can set PM2 so that it restarts your scripts after a reboot of your instance.

petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 2
    Why did I not think of this... Thank you so much for pointing this out. Much easier than what I was planning on doing. – AbnormalGeek Aug 28 '21 at 23:28