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 &