-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart-docker.sh
executable file
·54 lines (46 loc) · 1.74 KB
/
start-docker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
# Prefix text with timestamp (Example: 2024-11-19 08:08:03 My command output)
timestamp() {
echo "$(date -u -Iseconds | tr 'T' ' ' | cut -d'+' -f1)" "$@"
}
# Run command with optional output suppression
run_quiet() {
if [ -n "${QUIET}" ]; then
"$@" >/dev/null 2>&1
else
"$@"
fi
}
# Start a process and wait for a specific message
# Arguments:
# 1. Log file for output
# 2. Command to run the process
# 3. Regex indicating success when matched in output
# 3. Regex indicating failure when matched in output
wait_for_message_and_background() {
local process_log="$1"
local process_command="$2"
local success_regex="$3"
local failure_regex="$4"
timestamp "Starting '$process_command' ..."
touch "$process_log"
# Start process with timestamp
(timestamp "Process starting"; $process_command) > >(run_quiet tee "$process_log") 2>&1 &
local pid=$!
timestamp "Waiting for success message..."
while true; do
if grep -q "$success_regex" "$process_log"; then
break
fi
if grep -q "$failure_regex" "$process_log"; then
echo "Failure message detected. Exiting with failure."
exit 1
fi
sleep 0.1
done
# If the process is still running (detached), move it to the background
timestamp "Successfully started '$process_command' (pid: $pid) -> moving process to the background."
disown $pid
}
wait_for_message_and_background "containerd.log" "sudo containerd" "containerd successfully booted" "failed to start containerd"
wait_for_message_and_background "dockerd.log" "sudo dockerd -D --containerd /run/containerd/containerd.sock" "API listen on /var/run/docker.sock" "exit status\|failed to start containerd"