-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-when-available.sh
executable file
·61 lines (48 loc) · 1.61 KB
/
start-when-available.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
55
56
57
58
59
60
61
#!/usr/bin/env bash
DESCRIPTION="Start given command as soon as a url can be reached"
USAGE="Usage: start-when-available [OPTIONS][--delay time] [--batch] [--help|-h] url cmd
url
The url that needs to be available before executing the command
command
The command to be executed
Options:
-d, --delay time A minimum delay (in seconds) after which the command can be executed
-b, --batch Use batch mode for executing the command (can help with system resource
consumption)"
. "$(dirname $BASH_SOURCE)/lib/parse_args.sh"
REQUIRED=("url" "command")
KEYWORDS=("-d;int" "--delay;int" "-b;bool" "--batch;bool")
parse_args __USAGE "$USAGE" __DESCRIPTION "$DESCRIPTION" "$@"
set_trap 1 2
min_wait_time="${KW_ARGS['-d']-5}"
min_wait_time="${KW_ARGS['--delay']-$min_wait_time}"
url="${NAMED_ARGS['url']}"
cmd="${NAMED_ARGS['command']}"
batch_mode="${KW_ARGS['-b']-false}"
batch_mode="${KW_ARGS['--batch']-$batch_mode}"
if [[ "$batch_mode" == "true" ]]
then
[[ -z "$(command -v batch)" ]] && {
echo "ERROR: 'batch' command not found! Is the at package installed?"
exit 1
}
echo "cmd: $cmd";
cmd="${cmd//\'/\\\'}"
cmd="${cmd//\"/\\\"}"
echo "cmd escaped: $cmd"
batch_cmd="export DISPLAY=$DISPLAY; \"$0\" \"$url\" \"$cmd\" &"
msg="Executing on low system load: \"$batch_cmd\"..."
echo "$msg"
[[ -z "$(command -v notify-send)" ]] || notify-send "$msg"
echo "\"$batch_cmd\"" | cat
set -x
echo "$batch_cmd" | batch
set +x
exit 0
fi
sleep "$min_wait_time"
while ! curl "$url" &> /dev/null ;
do
sleep 1
done
bash -c "$cmd"