Search code examples
shellgoogle-chromeheadless-browsergoogle-chrome-headless

Chrome headless: exit on timeout


I'm using chrome headless to take screenshots. But when adding a timeout, the process does not exit, it stays alive... Is there a way to kill the process on errors (like timeouts)?

Command without timeout and its output (this one is working):

>>> chrome --headless --screenshot --no-sandbox https://stackoverflow.com
[0808/174530.681784:ERROR:gpu_process_transport_factory.cc(1016)] Lost UI shared context.
Fontconfig warning: "/etc/fonts/fonts.conf", line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf
[0808/174531.735502:ERROR:cert_verify_proc_nss.cc(981)] CERT_PKIXVerifyCert for securepubads.g.doubleclick.net failed err=-8179
[0808/174532.060892:INFO:headless_shell.cc(590)] Written to file screenshot.png.
>>> 

Command with timeout (at 1ms for testing) and its output (pending):

>>> chrome --headless --screenshot --no-sandbox --timeout=1 https://stackoverflow.com
[0808/174256.720003:ERROR:gpu_process_transport_factory.cc(1016)] Lost UI shared context.
Fontconfig warning: "/etc/fonts/fonts.conf", line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf
[0808/174256.772496:INFO:headless_shell.cc(344)] Timeout.

Solution

  • I prefered to use Linux command timeout. Here is the sample for you case, to wait 60 seconds then terminate the chrome instance.

    >>> timeout 60 chrome --headless --screenshot --no-sandbox https://stackoverflow.com