@@ -12,11 +12,11 @@ cat <<EOF
12
12
13
13
$NAME (PHP built-in web server manager) Version 0.1.0
14
14
PHP builtin server manager on port $PORT
15
-
15
+
16
16
usage: ./$NAME <command> [<hostname>:<port>]
17
17
18
18
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
20
20
stop Stops the PHP built-in web server
21
21
restart Stops and Starts on previously specified hostname:port
22
22
28
28
return 0
29
29
}
30
30
31
-
32
31
# if no command specified exit and show usage
33
32
if [[ $# < 1 ]]; then
34
33
echo $NAME : no command specified
51
50
PIDFILE=" $NAME " .pid
52
51
LOGFILE=" $NAME " .log
53
52
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
+ }
54
68
55
69
start_server () {
70
+
71
+ validate_server
72
+ if [[ $? -eq 1 ]]; then
73
+ return 1
74
+ fi
75
+
56
76
if [[ -e " $PIDFILE " ]]; then
57
77
echo Server seems to be running!
58
- echo
78
+ echo
59
79
echo if not, there is probably a zombie " $PIDFILE " in this directory.
60
80
echo if you are sure no server is running just remove " $PIDFILE " manually and start again
61
81
return 1
62
82
else
63
83
echo " $NAME " started on $HOST :$PORT
64
- ( php -S " $HOST " :" $PORT " >> " $LOGFILE " 2>&1 ) &
84
+ php -S " $HOST " :" $PORT " >> " $LOGFILE " 2>&1 &
65
85
echo " $HOST " :" $PORT " :$! > $PIDFILE
66
86
return 0
67
87
fi
68
88
}
69
89
70
90
stop_server () {
71
91
if [[ -e " $PIDFILE " ]]; then
72
- IFS=' :' read -r -a hostportpid <<< ` cat " $PIDFILE " `
92
+ IFS=' :' read -r -a hostportpid <<< ` cat " $PIDFILE " `
73
93
PID=${hostportpid[2]}
74
94
PORT=${hostportpid[1]}
75
95
HOST=${hostportpid[0]}
76
- kill " $PID "
77
- rm " $PIDFILE "
96
+ kill -9 " $PID "
97
+ rm -f " $PIDFILE "
78
98
echo " $NAME " stopped!
79
99
return 0
80
- else
100
+ else
81
101
echo " $NAME " is not running
82
102
return 1
83
103
fi
84
104
}
85
105
86
-
87
106
case $1 in
88
107
start) start_server;;
89
108
stop) stop_server;;
0 commit comments