Skip to content

Commit 97e5ae9

Browse files
authored
Merge pull request #1 from chapeupreto/validate_server
small improvements and add validate_server function
2 parents c59aced + 20e0e1f commit 97e5ae9

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

server

+29-10
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ cat <<EOF
1212
1313
$NAME (PHP built-in web server manager) Version 0.1.0
1414
PHP builtin server manager on port $PORT
15-
15+
1616
usage: ./$NAME <command> [<hostname>:<port>]
1717
1818
Available commands:
19-
start Starts PHP built-in web server server on specified hostname:port, default is localhost:$PORT
19+
start Starts PHP built-in web server server on specified hostname:port, default is localhost:$PORT
2020
stop Stops the PHP built-in web server
2121
restart Stops and Starts on previously specified hostname:port
2222
@@ -28,7 +28,6 @@ EOF
2828
return 0
2929
}
3030

31-
3231
# if no command specified exit and show usage
3332
if [[ $# < 1 ]]; then
3433
echo $NAME: no command specified
@@ -51,39 +50,59 @@ fi
5150
PIDFILE="$NAME".pid
5251
LOGFILE="$NAME".log
5352

53+
validate_server () {
54+
which php &> /dev/null
55+
if [[ $? -eq 1 ]]; then
56+
echo "Error: PHP not found. Please install PHP version 5.4 or greater"
57+
return 1
58+
fi
59+
60+
php -h | grep -q -- '-S'
61+
if [[ $? -eq 1 ]]; then
62+
echo "Error: PHP version must be 5.4 or greater"
63+
return 1
64+
fi
65+
66+
return 0
67+
}
5468

5569
start_server () {
70+
71+
validate_server
72+
if [[ $? -eq 1 ]]; then
73+
return 1
74+
fi
75+
5676
if [[ -e "$PIDFILE" ]]; then
5777
echo Server seems to be running!
58-
echo
78+
echo
5979
echo if not, there is probably a zombie "$PIDFILE" in this directory.
6080
echo if you are sure no server is running just remove "$PIDFILE" manually and start again
6181
return 1
6282
else
6383
echo "$NAME" started on $HOST:$PORT
64-
(php -S "$HOST":"$PORT" >> "$LOGFILE" 2>&1)&
84+
php -S "$HOST":"$PORT" >> "$LOGFILE" 2>&1 &
6585
echo "$HOST":"$PORT":$! > $PIDFILE
6686
return 0
6787
fi
6888
}
6989

7090
stop_server () {
7191
if [[ -e "$PIDFILE" ]]; then
72-
IFS=':' read -r -a hostportpid <<< `cat "$PIDFILE"`
92+
IFS=':' read -r -a hostportpid <<< `cat "$PIDFILE"`
7393
PID=${hostportpid[2]}
7494
PORT=${hostportpid[1]}
7595
HOST=${hostportpid[0]}
76-
kill "$PID"
77-
rm "$PIDFILE"
96+
kill -9 "$PID"
97+
rm -f "$PIDFILE"
7898
echo "$NAME" stopped!
7999
return 0
80-
else
100+
else
81101
echo "$NAME" is not running
82102
return 1
83103
fi
84104
}
85105
86-
87106
case $1 in
88107
start) start_server;;
89108
stop) stop_server;;

0 commit comments

Comments
 (0)