Home > Blockchain >  Chromedriver error when exiting an EC2 instance
Chromedriver error when exiting an EC2 instance

Time:11-21

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 &

CodePudding user response:

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 into your remote box,but leave your processes running.

CodePudding user response:

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

nohup ./script.py > foo.out 2> foo.err < /dev/null &

See the original answer here

  • Related