Skip to content
This repository was archived by the owner on Jun 22, 2024. It is now read-only.

Commit 3909301

Browse files
Added cleanup script for hanging Chrome processes and temp files (SeleniumHQ#2173)
1 parent 6f03eb1 commit 3909301

File tree

11 files changed

+220
-0
lines changed

11 files changed

+220
-0
lines changed

NodeBase/Dockerfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ RUN chmod -R 775 ${HOME} /tmp/.X11-unix \
133133
&& chgrp -R 0 ${HOME} /tmp/.X11-unix \
134134
&& chmod -R g=u ${HOME} /tmp/.X11-unix
135135

136+
#============================================
137+
# Shared cleanup script environment variables
138+
#============================================
139+
ENV SE_ENABLE_BROWSER_LEFTOVERS_CLEANUP false
140+
ENV SE_BROWSER_LEFTOVERS_INTERVAL_SECS 3600
141+
ENV SE_BROWSER_LEFTOVERS_PROCESSES_SECS 1200
142+
ENV SE_BROWSER_LEFTOVERS_TEMPFILES_DAYS 1
143+
136144
#===================================================
137145
# Run the following commands as non-privileged user
138146
#===================================================

NodeChrome/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ RUN if [ ! -z "$CHROME_DRIVER_VERSION" ]; \
5353
&& chmod 755 /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION \
5454
&& ln -fs /opt/selenium/chromedriver-$CHROME_DRIVER_VERSION /usr/bin/chromedriver
5555

56+
#============================================
57+
# Chrome cleanup script and supervisord file
58+
#============================================
59+
COPY chrome-cleanup.sh /opt/bin/chrome-cleanup.sh
60+
COPY chrome-cleanup.conf /etc/supervisor/conf.d/chrome-cleanup.conf
61+
5662
USER ${SEL_UID}
5763

5864
#============================================

NodeChrome/chrome-cleanup.conf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; Documentation of this file format -> http://supervisord.org/configuration.html
2+
3+
; Priority 0 - xvfb & fluxbox, 5 - x11vnc, 10 - noVNC, 15 - selenium-node
4+
5+
[program:browserleftoverscleanup]
6+
priority=0
7+
command=bash -c "if [ ${SE_ENABLE_BROWSER_LEFTOVERS_CLEANUP} = "true" ]; then /opt/bin/chrome-cleanup.sh; fi"
8+
autostart=true
9+
exitcodes=0
10+
autorestart=unexpected
11+
12+
;Logs
13+
redirect_stderr=false
14+
stdout_logfile=/var/log/supervisor/browser-leftover-cleanup-stdout.log
15+
stderr_logfile=/var/log/supervisor/browser-leftover-cleanup-stderr.log
16+
stdout_logfile_maxbytes=50MB
17+
stderr_logfile_maxbytes=50MB
18+
stdout_logfile_backups=5
19+
stderr_logfile_backups=5
20+
stdout_capture_maxbytes=50MB
21+
stderr_capture_maxbytes=50MB

NodeChrome/chrome-cleanup.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# Return error exit code in case of any failure, so supervisord will restart the script
4+
set -e
5+
6+
cleanup_stuck_chrome_processes() {
7+
echo -n "Killing Chrome processes older than ${SE_BROWSER_LEFTOVERS_PROCESSES_SECS} seconds... "
8+
ps -e -o pid,etimes,command | grep -v grep | grep chrome/chrome | awk '{if($2>'${SE_BROWSER_LEFTOVERS_PROCESSES_SECS}') print $0}' | awk '{print $1}' | xargs -r kill -9
9+
echo "DONE."
10+
}
11+
12+
cleanup_tmp_chrome_files() {
13+
echo -n "Deleting all Chrome files in /tmp... "
14+
find /tmp -name ".com.google.Chrome.*" -type d -mtime +${SE_BROWSER_LEFTOVERS_TEMPFILES_DAYS} -exec rm -rf "{}" +
15+
echo "DONE."
16+
}
17+
18+
echo "Chrome cleanup script init with parameters: SE_BROWSER_LEFTOVERS_TEMPFILES_DAYS=${SE_BROWSER_LEFTOVERS_TEMPFILES_DAYS}, SE_BROWSER_LEFTOVERS_PROCESSES_SECS=${SE_BROWSER_LEFTOVERS_PROCESSES_SECS}, SE_BROWSER_LEFTOVERS_INTERVAL_SECS=${SE_BROWSER_LEFTOVERS_INTERVAL_SECS}."
19+
20+
# Start the main loop
21+
while :
22+
do
23+
echo "Starting cleanup daemon script."
24+
25+
# Clean up stuck processes
26+
cleanup_stuck_chrome_processes
27+
28+
# Wait a few seconds for the processes to stop before removing files
29+
sleep 5
30+
31+
# Clean up temporary files
32+
cleanup_tmp_chrome_files
33+
34+
# Go to sleep for 1 hour
35+
echo "Cleanup daemon sleeping for ${SE_BROWSER_LEFTOVERS_INTERVAL_SECS} seconds."
36+
sleep ${SE_BROWSER_LEFTOVERS_INTERVAL_SECS}
37+
done

NodeEdge/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ RUN if [ -z "$EDGE_DRIVER_VERSION" ]; \
4646
&& chmod 755 /opt/selenium/msedgedriver-$EDGE_DRIVER_VERSION \
4747
&& ln -fs /opt/selenium/msedgedriver-$EDGE_DRIVER_VERSION /usr/bin/msedgedriver
4848

49+
#============================================
50+
# Edge cleanup script and supervisord file
51+
#============================================
52+
COPY edge-cleanup.sh /opt/bin/edge-cleanup.sh
53+
COPY edge-cleanup.conf /etc/supervisor/conf.d/edge-cleanup.conf
54+
4955
USER ${SEL_UID}
5056

5157
#============================================

NodeEdge/edge-cleanup.conf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; Documentation of this file format -> http://supervisord.org/configuration.html
2+
3+
; Priority 0 - xvfb & fluxbox, 5 - x11vnc, 10 - noVNC, 15 - selenium-node
4+
5+
[program:browserleftoverscleanup]
6+
priority=0
7+
command=bash -c "if [ ${SE_ENABLE_BROWSER_LEFTOVERS_CLEANUP} = "true" ]; then /opt/bin/edge-cleanup.sh; fi"
8+
autostart=true
9+
exitcodes=0
10+
autorestart=unexpected
11+
12+
;Logs
13+
redirect_stderr=false
14+
stdout_logfile=/var/log/supervisor/browser-leftover-cleanup-stdout.log
15+
stderr_logfile=/var/log/supervisor/browser-leftover-cleanup-stderr.log
16+
stdout_logfile_maxbytes=50MB
17+
stderr_logfile_maxbytes=50MB
18+
stdout_logfile_backups=5
19+
stderr_logfile_backups=5
20+
stdout_capture_maxbytes=50MB
21+
stderr_capture_maxbytes=50MB

NodeEdge/edge-cleanup.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# Return error exit code in case of any failure, so supervisord will restart the script
4+
set -e
5+
6+
cleanup_stuck_edge_processes() {
7+
echo -n "Killing Edge processes older than ${SE_BROWSER_LEFTOVERS_PROCESSES_SECS} seconds... "
8+
ps -e -o pid,etimes,command | grep -v grep | grep msedge/msedge | awk '{if($2>'${SE_BROWSER_LEFTOVERS_PROCESSES_SECS}') print $0}' | awk '{print $1}' | xargs -r kill -9
9+
echo "DONE."
10+
}
11+
12+
cleanup_tmp_edge_files() {
13+
echo -n "Deleting all Edge files in /tmp... "
14+
find /tmp -name ".com.microsoft.Edge.*" -type d -mtime +${SE_BROWSER_LEFTOVERS_TEMPFILES_DAYS} -exec rm -rf "{}" +
15+
echo "DONE."
16+
}
17+
18+
echo "Edge cleanup script init with parameters: SE_BROWSER_LEFTOVERS_TEMPFILES_DAYS=${SE_BROWSER_LEFTOVERS_TEMPFILES_DAYS}, SE_BROWSER_LEFTOVERS_PROCESSES_SECS=${SE_BROWSER_LEFTOVERS_PROCESSES_SECS}, SE_BROWSER_LEFTOVERS_INTERVAL_SECS=${SE_BROWSER_LEFTOVERS_INTERVAL_SECS}."
19+
20+
# Start the main loop
21+
while :
22+
do
23+
echo "Starting cleanup daemon script."
24+
25+
# Clean up stuck processes
26+
cleanup_stuck_edge_processes
27+
28+
# Wait a few seconds for the processes to stop before removing files
29+
sleep 5
30+
31+
# Clean up temporary files
32+
cleanup_tmp_edge_files
33+
34+
# Go to sleep for 1 hour
35+
echo "Cleanup daemon sleeping for ${SE_BROWSER_LEFTOVERS_INTERVAL_SECS} seconds."
36+
sleep ${SE_BROWSER_LEFTOVERS_INTERVAL_SECS}
37+
done

NodeFirefox/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ RUN GK_VERSION=$(if [ ${GECKODRIVER_VERSION:-latest} = "latest" ]; then echo "0.
3636
&& chmod 755 /opt/geckodriver-$GK_VERSION \
3737
&& ln -fs /opt/geckodriver-$GK_VERSION /usr/bin/geckodriver
3838

39+
#============================================
40+
# Firefox cleanup script and supervisord file
41+
#============================================
42+
COPY firefox-cleanup.sh /opt/bin/firefox-cleanup.sh
43+
COPY firefox-cleanup.conf /etc/supervisor/conf.d/firefox-cleanup.conf
44+
3945
USER ${SEL_UID}
4046

4147
#============================================

NodeFirefox/firefox-cleanup.conf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; Documentation of this file format -> http://supervisord.org/configuration.html
2+
3+
; Priority 0 - xvfb & fluxbox, 5 - x11vnc, 10 - noVNC, 15 - selenium-node
4+
5+
[program:browserleftoverscleanup]
6+
priority=0
7+
command=bash -c "if [ ${SE_ENABLE_BROWSER_LEFTOVERS_CLEANUP} = "true" ]; then /opt/bin/firefox-cleanup.sh; fi"
8+
autostart=true
9+
exitcodes=0
10+
autorestart=unexpected
11+
12+
;Logs
13+
redirect_stderr=false
14+
stdout_logfile=/var/log/supervisor/browser-leftover-cleanup-stdout.log
15+
stderr_logfile=/var/log/supervisor/browser-leftover-cleanup-stderr.log
16+
stdout_logfile_maxbytes=50MB
17+
stderr_logfile_maxbytes=50MB
18+
stdout_logfile_backups=5
19+
stderr_logfile_backups=5
20+
stdout_capture_maxbytes=50MB
21+
stderr_capture_maxbytes=50MB

NodeFirefox/firefox-cleanup.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
# Return error exit code in case of any failure, so supervisord will restart the script
4+
set -e
5+
6+
cleanup_stuck_firefox_processes() {
7+
echo -n "Killing Firefox processes older than ${SE_BROWSER_LEFTOVERS_PROCESSES_SECS} seconds... "
8+
ps -e -o pid,etimes,command | grep -v grep | grep firefox-bin | awk '{if($2>'${SE_BROWSER_LEFTOVERS_PROCESSES_SECS}') print $0}' | awk '{print $1}' | xargs -r kill -9
9+
echo "DONE."
10+
}
11+
12+
echo "Firefox cleanup script init with parameters: SE_BROWSER_LEFTOVERS_PROCESSES_SECS=${SE_BROWSER_LEFTOVERS_PROCESSES_SECS}, SE_BROWSER_LEFTOVERS_INTERVAL_SECS=${SE_BROWSER_LEFTOVERS_INTERVAL_SECS}."
13+
14+
# Start the main loop
15+
while :
16+
do
17+
echo "Starting cleanup daemon script."
18+
19+
# Clean up stuck processes
20+
cleanup_stuck_firefox_processes
21+
22+
# Go to sleep for 1 hour
23+
echo "Cleanup daemon sleeping for ${SE_BROWSER_LEFTOVERS_INTERVAL_SECS} seconds."
24+
sleep ${SE_BROWSER_LEFTOVERS_INTERVAL_SECS}
25+
done

0 commit comments

Comments
 (0)