as @DebanjanB answer is correct, but not clear.
After fixed issue and summary solution to here:
Issue: run selenium, but error
code:
from selenium import webdriver
chromeOptions = webdriver.ChromeOptions()
chromeOptions.add_argument('--headless’)
driver = webdriver.Chrome(options=chromeOptions)
run but error:
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Cause
direct cause
unknown error: DevToolsActivePort file doesn't exist
means ChromeDriver spawn WebBrowser (Chrome Browser session) failed
root cause
- use ROOT to run Chrome, will cause this error
Solution
(recommended) correct solution
use NORMAL USER to run Chrome
how to use normal user to run chrome?
my case: in CentOS
(1) change chrome binary to own by normal user
sudo chown limao:limao /opt/google/chrome/google-chrome
note: here chrome binary is /usr/bin/google-chrome
after several soft link to real one is /opt/google/chrome/google-chrome
after change owner:
limao@localhost:~/dev/ShortLinkParseServer/logs $ ll /usr/bin/google-chrome
lrwxrwxrwx 1 root root 31 Aug 4 16:30 /usr/bin/google-chrome -> /etc/alternatives/google-chrome
limao@localhost:~/dev/ShortLinkParseServer/logs $ ll /etc/alternatives/google-chrome
lrwxrwxrwx 1 root root 29 Aug 4 16:30 /etc/alternatives/google-chrome -> /usr/bin/google-chrome-stable
limao@localhost:~/dev/ShortLinkParseServer/logs $ ll /opt/google/chrome/google-chrome
-rwxr-xr-x 1 limao limao 1.9K Jul 31 04:46 /opt/google/chrome/google-chrome
(2) set normal user for supervisord spawn program
add user=xxx
into your supervisord config file
, like this:
$ cat /etc/supervisord.d/supervisord_ShortLinkParseServer.conf
[program:ShortLinkParseServer]
command=/xxx/gunicorn/gunicorn_config.py app:app
...
; use normal user instead default root use, to avoid later chrome exception: unknown error: DevToolsActivePort file doesn't exist
user=limao
...
workaround solution (to be verified)
add flag --disable-dev-shm-usage
chromeOptions.add_argument('--disable-dev-shm-usage')
wrong solution
too much people used this: add flag --no-sandbox
but --no-sandbox
just means not use sandbox
-> true effect is just bypass OS security model
-> so highly NOT recommend to use --no-sandbox