Skip to content

Commit 492e8e5

Browse files
committed
Improve restart reliability
1 parent 6ab27c4 commit 492e8e5

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

.devcontainer/bin/init-dev-container.sh

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ startInBackgroundIfNotRunning()
88
{
99
log "Starting $1."
1010
echo -e "\n** $(date) **" | sudoIf tee -a /tmp/$1.log > /dev/null
11-
if [ ! -f "/tmp/$1.pid" ] || ! ps -p $(cat /tmp/$1.pid) > /dev/null; then
12-
($2 sh -c "while :; do echo [\$(date)] Process started.; $3; echo [\$(date)] Process exited!; sleep 5; done 2>&1" | sudoIf tee -a /tmp/$1.log > /dev/null & echo "$!" | sudoIf tee /tmp/$1.pid > /dev/null)
13-
log "$1 started."
11+
if ! pidof $1 > /dev/null; then
12+
keepRunningInBackground "$@"
1413
else
1514
echo "$1 is already running." | sudoIf tee -a /tmp/$1.log > /dev/null
1615
log "$1 is already running."
1716
fi
1817
}
1918

19+
# Keep command running in background
20+
keepRunningInBackground()
21+
{
22+
($2 sh -c "while :; do echo [\$(date)] Process started.; $3; echo [\$(date)] Process exited!; sleep 5; done 2>&1" | sudoIf tee -a /tmp/$1.log > /dev/null & echo "$!" | sudoIf tee /tmp/$1.pid > /dev/null)
23+
while ! pidof $1 > /dev/null; do
24+
sleep 1
25+
done
26+
log "$1 started."
27+
}
28+
2029
# Use sudo to run as root when required
2130
sudoIf()
2231
{
@@ -47,7 +56,7 @@ log "** SCRIPT START **"
4756

4857
# Start dbus.
4958
log 'Running "/etc/init.d/dbus start".'
50-
if [ -f "/var/run/dbus/pid" ] && ! ps -p $(cat /var/run/dbus/pid) > /dev/null; then
59+
if [ -f "/var/run/dbus/pid" ] && ! pidof dbus-daemon > /dev/null; then
5160
sudoIf rm -f /var/run/dbus/pid
5261
fi
5362
sudoIf /etc/init.d/dbus start 2>&1 | sudoIf tee -a /tmp/dbus-daemon-system.log > /dev/null
@@ -57,31 +66,19 @@ done
5766

5867
# Set up Xvfb.
5968
startInBackgroundIfNotRunning "Xvfb" sudoIf "Xvfb ${DISPLAY:-:1} +extension RANDR -screen 0 ${MAX_VNC_RESOLUTION:-1920x1080x16}"
60-
while ! pidof Xvfb > /dev/null; do
61-
sleep 1
62-
done
6369

6470
# Start fluxbox as a light weight window manager.
6571
startInBackgroundIfNotRunning "fluxbox" sudoUserIf "dbus-launch startfluxbox"
66-
while ! pidof fluxbox > /dev/null; do
67-
sleep 1
68-
done
6972

7073
# Set resolution
7174
/usr/local/bin/set-resolution ${VNC_RESOLUTION:-1280x720} ${VNC_DPI:-72}
7275

73-
# Start x11vnc if installed.
74-
if type x11vnc 2>&1 > /dev/null; then
75-
startInBackgroundIfNotRunning "x11vnc" sudoIf "x11vnc -display ${DISPLAY:-:1} -rfbport ${VNC_PORT:-5901} -listen localhost -rfbportv6 ${VNC_PORT:-5901} -listenv6 localhost -xkb -shared -forever -nopw"
76-
else
77-
log "Skipping x11vnc - not installed."
78-
fi
76+
# Start x11vnc
77+
startInBackgroundIfNotRunning "x11vnc" sudoIf "x11vnc -display ${DISPLAY:-:1} -rfbport ${VNC_PORT:-5901} -localhost -no6 -xkb -shared -forever -nopw"
7978

80-
# Spin up noVNC if installed.
81-
if [ -d "/usr/local/novnc" ]; then
82-
startInBackgroundIfNotRunning "noVNC" sudoIf "/usr/local/novnc/noVNC*/utils/launch.sh --listen ${NOVNC_PORT:-6080} --vnc localhost:${VNC_PORT:-5901}"
83-
else
84-
log "Skipping noVNC - not installed."
79+
# Spin up noVNC if installed and not runnning.
80+
if [ -d "/usr/local/novnc" ] && [ "$(ps -ef | grep /usr/local/novnc/noVNC*/utils/launch.sh)" = "" ]; then
81+
keepRunningInBackground "noVNC" sudoIf "/usr/local/novnc/noVNC*/utils/launch.sh --listen ${NOVNC_PORT:-6080} --vnc localhost:${VNC_PORT:-5901}"
8582
fi
8683

8784
# Run whatever was passed in

0 commit comments

Comments
 (0)