I’m trying to run a really simple script on an Ubuntu EC2 machine with Selenium.
I put the next piece of code inside a loop since the script should run in the background forever:
from selenium import webdriver
def play():
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("enable-automation")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-dev-shm-usage")
try:
driver = webdriver.Chrome(executable_path='/usr/bin/chromedriver', options=chrome_options)
except Exception as e:
with open(f'{os.getcwd()}/error_log.txt', 'a') as f:
f.write(str(datetime.datetime.now()))
f.write(str(e))
While connected to the instance with ssh, the script runs perfectly, but when disconnected, I get this error:
Message: Service /usr/bin/chromedriver unexpectedly exited. Status code was: 1
After re-connecting, the script works normally again with no touch.
I’m running the script as follow:
nohup python3 script.py &
2
Answers
When you run a process from ssh, it is bound to your terminal session so as soon as you close the session, all subordinate processes are terminated.
There are number of options how to deal. Nearly all of them implies that you have some additional tools installed and might be specific for your particular OS.
Here are nice threads about the issue:
https://serverfault.com/questions/463366/does-getting-disconnected-from-an-ssh-session-kill-your-programs
https://askubuntu.com/questions/8653/how-to-keep-processes-running-after-ending-ssh-session
https://superuser.com/questions/1293298/how-to-detach-ssh-session-without-killing-a-running-process#:~:text=ssh%20into%20your%20remote%20box,but%20leave%20your%20processes%20running.
The command you are running is attached to your shell session. In order to keep the script running, make use of nohup, and this should allow the process to continue even after you have disconnected from your shell session.
Try the following when you are on the machine
See the original answer here