0

I'm running a chatbot on Ubuntu server on Amazon EC2 instance. I want to be able to run the python3 program even after closing the pUTTY window. So far I've tried 'Ctrl+a, d' as well as 'Ctrl+z, bg'. However both methods did not seem to work after closing the pUTTY window. I referenced the following youtube video:

Would really appreciate any help! Chatbot Output Ubuntu Screen running python program

hengjuice
  • 112
  • 1
  • 1
  • 9
  • Does this answer your question? [How to run Node.js as a background process and never die?](https://stackoverflow.com/questions/4797050/how-to-run-node-js-as-a-background-process-and-never-die) – frippe Oct 13 '21 at 08:01
  • **DO NOT post images of code, data, error messages, etc.** - copy or type the text into the question. [ask] – Rob Oct 13 '21 at 08:55
  • There are no error messages sir. Apologies just starting out... – hengjuice Oct 13 '21 at 09:32

1 Answers1

1

I hope the question is "how to keep the python script running even after closing the putty"

IMO, you can use 2 approaches here.

Use nohup The unix command nohup can make your program run in background even after exiting the terminal. You can run it like

nohup python3 LP_poolVol.py > /dev/null &

nohup will make the script running and the & at the end will make it in background

Make the script a service

Run the script as a linux service which you can start and stop using the systemctl You can create a service descriptor file pool-vol.service with contents similar to below.

[Unit]
Description=Pool Vol Service
After=multi-user.target

[Service]
Type=simple
Restart=always
ExecStart=/usr/bin/python3 <path-to>/LP_poolVol.py

[Install]
WantedBy=multi-user.target

Then copy this service file to /etc/systemd/system/. And then install it via the commands below

sudo systemctl daemon-reload
sudo systemctl enable pool-vol.service
sudo systemctl start pool-vol

Now your app is running as a service. You can stop or restart it using the systemctl itself like

sudo systemctl start pool-vol
Kris
  • 8,680
  • 4
  • 39
  • 67