-
Notifications
You must be signed in to change notification settings - Fork 2
/
loader.sh
134 lines (117 loc) · 3.59 KB
/
loader.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
# JATOS loader for Linux and MacOS X
# Change IP address and port here
# Alternatively you can use command-line arguments -Dhttp.address and -Dhttp.port
address="127.0.0.1"
port="9000"
# Don't change after here unless you know what you're doing
###########################################################
# Get JATOS directory
dir="$( cd "$( dirname "$0" )" && pwd )"
pidfile="$dir/RUNNING_PID"
args=${@:2}
function start() {
getJavaArgs
checkAlreadyRunning
checkJava
echo -n "Starting JATOS"
# Generate application secret for the Play framework
secret="$(LC_ALL=C tr -cd '[:alnum:]' < /dev/urandom 2>/dev/null | dd bs=64 count=1 2>/dev/null)"
if [ ! -f "$dir/bin/jatos" ]
then
echo -e "\n$dir/bin/jatos doesn't exist!"
exit 1
fi
# In case './bin/jatos' isn't executable set the x bit
chmod u+x "$dir/bin/jatos"
# Start JATOS with configuration file and application secret
"$dir/bin/jatos" -Dconfig.file="$dir/conf/production.conf" -Dplay.crypto.secret=$secret -Dhttp.port=$port -Dhttp.address=$address -J-server $args > /dev/null &
echo "...started"
echo "To use JATOS type $address:$port in your browser's address bar"
}
function stop() {
if [ ! -f "$pidfile" ] || ! kill -0 $(cat "$pidfile") 2>&1 >/dev/null
then
echo "This JATOS was not running"
exit 1
fi
echo -n "Stopping JATOS"
kill -SIGTERM $(cat "$pidfile")
rm -f "$pidfile"
echo "...stopped"
}
function getJavaArgs() {
for i in $args ; do
case $i in
-Dhttp.address=*)
address=${i#*=}
;;
-Dhttp.port=*)
port=${i#*=}
;;
esac
done
}
function checkAlreadyRunning() {
if [ -f "$pidfile" ] && kill -0 $(cat "$pidfile") 2>&1 >/dev/null
then
echo "There seems to be a running JATOS."
exit 1
fi
# Delete old PID file (just to be sure)
rm -f "$pidfile"
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null
then
echo "Some other program is using port $port already. Maybe another JATOS?"
exit 1
fi
}
function checkJava() {
# Get Java version if installed
if type -p java
then
echo "Found Java"
java_version=$(java -version 2>&1 | sed 's/.*version "\(.*\)\.\(.*\)\..*"/\1\2/; 1q')
elif [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]
then
echo "Found Java in JAVA_HOME"
java_version=$($JAVA_HOME/bin/java -version 2>&1 | sed 's/.*version "\(.*\)\.\(.*\)\..*"/\1\2/; 1q')
fi
# If we don't have a version or if the version is not a number or if the version is smaller 18 (Java 8) try to find a local Java installation
if [[ -z "$java_version" ]] || [[ ! "$java_version" =~ ^[0-9]+$ ]] || [[ "$java_version" -lt 18 ]]
then
if [[ -n "$dir/jre/linux_x64_jre" ]] && [[ -x "$dir/jre/linux_x64_jre/bin/java" ]]
then
echo "Jatos uses local JRE"
export JAVA_HOME="$dir/jre/linux_x64_jre"
elif [[ -n "$dir/jre/mac_x64_jre" ]] && [[ -x "$dir/jre/mac_x64_jre/bin/java" ]]
then
echo "Jatos uses local JRE"
export JAVA_HOME="$dir/jre/mac_x64_jre"
else
echo "No Java found"
exit 1
fi
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
# Check that JATOS' port is free
while lsof -Pi :$port -sTCP:LISTEN -t >/dev/null
do
sleep 1
done
start
;;
*)
echo "Usage: loader.sh start|stop|restart"
exit 1
;;
esac