-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverStart.sh
More file actions
executable file
·94 lines (72 loc) · 2.73 KB
/
serverStart.sh
File metadata and controls
executable file
·94 lines (72 loc) · 2.73 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
# Copyright (c) 2025 Black Duck Software. All rights reserved worldwide.
# Start the process specified in startCmd argument
# Create a log file in /tmp
# Watch the log for serverStarted string and return the PID of the running process
# Note: Make sure you are specifying the correct string to search for. Note: Maven will add its own colour which
# can cause the search string to not be found.
wait_str() {
local file="$1"; shift
local search_term="$1"; shift
local wait_time="${1:-5m}"; shift # 5 minutes as default timeout
(timeout $wait_time tail -F -n0 "$file" &) | grep -q "$search_term" && return 0
echo "Timeout of $wait_time reached. Unable to find '$search_term' in '$file'"
return 1
}
wait_server() {
local server_log="$1"; shift
local started_text="$1"; shift
local wait_time="$1"; shift
wait_file "$server_log" 10 || { echo "Server log file missing: '$server_log'"; return 1; }
wait_str "$server_log" "$started_text" "$wait_time"
}
wait_file() {
local file="$1"; shift
local wait_seconds="${1:-10}"; shift # 10 seconds as default timeout
until test $((wait_seconds--)) -eq 0 -o -f "$file" ; do sleep 1; done
((++wait_seconds))
}
for i in "$@"; do
case "$i" in
--startCmd=*) startCmd="${i#*=}" ;;
--startedString=*) startedString="${i#*=}" ;;
--project=*) project="${i#*=}" ;;
--timeout=*) timeout="${i#*=}" ;;
esac
done
if [ -z "$startCmd" ]; then
echo "You must specify startCmd"
echo "Usage: serverStart.sh --startCmd=COMMAND_TO_START_SERVER --startedString=SERVER_STARTED_MESSAGE --project=PROJECT --timeout=TIMEOUT"
exit 1
fi
if [ -z "$startedString" ]; then
echo "You must specify startedString"
echo "Usage: serverStart.sh --startCmd=COMMAND_TO_START_SERVER --startedString=SERVER_STARTED_MESSAGE --project=PROJECT --timeout=TIMEOUT"
exit 1
fi
if [ -z "$project" ]; then
echo "You must specify project"
echo "Usage: serverStart.sh --startCmd=COMMAND_TO_START_SERVER --startedString=SERVER_STARTED_MESSAGE --project=PROJECT --timeout=TIMEOUT"
exit 1
fi
if [ -z "$timeout" ]; then
echo "You must specify a timeout"
echo "Usage: serverStart.sh --startCmd=COMMAND_TO_START_SERVER --startedString=SERVER_STARTED_MESSAGE --project=PROJECT --timeout=TIMEOUT"
exit 1
fi
#Split $startCmd into operation and operands
commandArray=($startCmd)
startOperation=$commandArray
for (( n=1; n < ${#commandArray[*]}; n++))
do
startOperands="$startOperands ${commandArray[n]}"
done
output=$(mktemp /tmp/$project.XXX)
($startOperation $startOperands >$output 2>$output) &
serverPID=$!
if (! wait_server $output "$startedString" $timeout); then
echo "Could not start server"
exit 1
fi
echo $serverPID
exit 0