Skip to content

Commit

Permalink
Refactor start_chrome_headless (#22257)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkatzir authored Dec 10, 2023
1 parent e71c7b3 commit 174318d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 48 deletions.
44 changes: 20 additions & 24 deletions docker/chromium/start_chrome_headless.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
#!/bin/bash

ignore_certificate_errors="--ignore-certificate-errors"
# Default values (consider injecting them from the outside as arguments)
ignore_certificate_errors=true
chrome_binary="/opt/google/chrome/google-chrome"
remote_debugging_port=9222
max_attempts=3
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36"

while [[ "$#" -gt 0 ]]; do
case "${1}" in
--validate-certificates) ignore_certificate_errors=""
shift;;
*) # unknown option.
shift
echo "Unknown option was received: $1"
;;
esac
case "${1}" in
--validate-certificates) ignore_certificate_errors=false ;;
--chrome-binary) chrome_binary="${2}"; shift ;;
--user-agent) user_agent="${2}"; shift ;;
--port) remote_debugging_port="${2}"; shift ;;
*) echo "Unknown option: ${1}" ;;
esac
shift
done

echo "ignore_certificate_errors: ${ignore_certificate_errors}"
local cert_errors_option=""
if [ "$ignore_certificate_errors" = true ]; then
cert_errors_option="--ignore-certificate-errors"
fi

for value in {1..3}
do
echo "Trying to start chrome, attempt $value"
ps -aux | grep "chrome" | grep "9222" > /dev/null 2>&1
if [[ "$?" == "0" ]]; then
echo "Chrome is running"
exit 0
else
echo "Chrome is not running"
service dbus restart
nohup /opt/google/chrome/google-chrome --headless --disable-gpu --no-sandbox --hide-scrollbars --disable_infobars --start-maximized --start-fullscreen "${ignore_certificate_errors}" --disable-dev-shm-usage --user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36" --remote-debugging-port=9222 & disown
fi
sleep 3
done
echo "Starting Chrome..."
nohup "$chrome_binary" --headless --disable-gpu --no-sandbox --hide-scrollbars --disable-infobars --start-maximized --start-fullscreen $cert_errors_option --disable-dev-shm-usage --user-agent="\"$user_agent\"" --remote-debugging-port="$remote_debugging_port" & disown
44 changes: 20 additions & 24 deletions docker/py3-native/chromium/start_chrome_headless.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
#!/bin/bash

ignore_certificate_errors="--ignore-certificate-errors"
# Default values (consider injecting them from the outside as arguments)
ignore_certificate_errors=true
chrome_binary="/opt/google/chrome/google-chrome"
remote_debugging_port=9222
max_attempts=3
user_agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36"

while [[ "$#" -gt 0 ]]; do
case "${1}" in
--validate-certificates) ignore_certificate_errors=""
shift;;
*) # unknown option.
shift
echo "Unknown option was received: $1"
;;
esac
case "${1}" in
--validate-certificates) ignore_certificate_errors=false ;;
--chrome-binary) chrome_binary="${2}"; shift ;;
--user-agent) user_agent="${2}"; shift ;;
--port) remote_debugging_port="${2}"; shift ;;
*) echo "Unknown option: ${1}" ;;
esac
shift
done

echo "ignore_certificate_errors: ${ignore_certificate_errors}"
local cert_errors_option=""
if [ "$ignore_certificate_errors" = true ]; then
cert_errors_option="--ignore-certificate-errors"
fi

for value in {1..3}
do
echo "Trying to start chrome, attempt $value"
ps -aux | grep "chrome" | grep "9222" > /dev/null 2>&1
if [[ "$?" == "0" ]]; then
echo "Chrome is running"
exit 0
else
echo "Chrome is not running"
service dbus restart
nohup /opt/google/chrome/google-chrome --headless --disable-gpu --no-sandbox --hide-scrollbars --disable_infobars --start-maximized --start-fullscreen "${ignore_certificate_errors}" --disable-dev-shm-usage --user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36" --remote-debugging-port=9222 & disown
fi
sleep 3
done
echo "Starting Chrome..."
nohup "$chrome_binary" --headless --disable-gpu --no-sandbox --hide-scrollbars --disable-infobars --start-maximized --start-fullscreen $cert_errors_option --disable-dev-shm-usage --user-agent="\"$user_agent\"" --remote-debugging-port="$remote_debugging_port" & disown

0 comments on commit 174318d

Please sign in to comment.