Home > Software design >  How to detach process in Ubuntu
How to detach process in Ubuntu

Time:10-14

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

CodePudding user response:

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
  • Related