mysql,nginx,redis,php一键启动脚本
1 #!/bin/bash 2 php='/usr/local/php/sbin/php-fpm' 3 php_conf="/usr/local/php/etc/php-fpm.conf" 4 nginx="/usr/local/nginx/sbin/nginx" 5 nginx_conf="/usr/local/nginx/conf/nginx.conf" 6 mysql_pid_file="/usr/local/mysql/data/"`uname -n`.pid 7 mysql="/usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=$mysql_pid_file" 8 redis="/usr/local/redis/bin/redis-server" 9 redis_conf="/usr/local/redis/redis.conf" 10 timeout=30 11 i=0 12 13 wait_for_pid(){ 14 i=0 15 always=1 16 while [[ $i -lt $timeout && $always = 1 ]] 17 do 18 if test $1 = php 19 then 20 pid=`ps aux | grep '[0-9] php-fpm: master'|awk '{print $2}'` 21 elif test $1 = nginx 22 then 23 pid=`ps aux | grep '[0-9] nginx: master'|awk '{print $2}'` 24 elif test $1 = mysql 25 then 26 pid= 27 if test -f $mysql_pid_file 28 then 29 pid=`cat $mysql_pid_file 2>/dev/null` 30 fi 31 elif test $1 = redis 32 then 33 pid=`ps aux | grep 'redis-server \*:6379'|awk '{print $2}'` 34 fi 35 if [[ $2 = 1 && -n "$pid" ]] 36 then 37 break 38 elif [[ $2 = 0 && -z "$pid" ]] 39 then 40 break 41 fi 42 let i+=1 43 if [[ -n "$3" && $3 -gt 0 ]] 44 then 45 always=0 46 fi 47 sleep 1 48 done 49 echo -n $pid 50 51 } 52 53 54 case $2 in 55 56 start) 57 if [[ $1 = php || $1 = all ]] 58 then 59 pid=`wait_for_pid php 1 1` 60 if test -n "$pid" 61 then 62 echo -e "\033[32m php-fpm started \033[0m" 63 else 64 $php -y $php_conf 2>&1 & 65 echo -n "php-fpm starting" 66 pid=`wait_for_pid php 1` 67 if test -n "$pid" 68 then 69 echo -e "\033[32m [ok] \033[0m" 70 else 71 echo -e "\033[31m [ok] \033[0m" 72 fi 73 fi 74 fi 75 if [[ $1 = nginx || $1 = all ]] 76 then 77 pid=`wait_for_pid nginx 1 1` 78 if test -n "$pid" 79 then 80 echo -e "\033[32m nginx started \033[0m" 81 else 82 $nginx -c $nginx_conf 2>&1 & 83 echo -n "nginx starting" 84 pid=`wait_for_pid nginx 1` 85 if test -n "$pid" 86 then 87 echo -e "\033[32m [ok] \033[0m" 88 else 89 echo -e "\033[31m [ok] \033[0m" 90 fi 91 92 fi 93 fi 94 if [[ $1 = mysql || $1 = all ]] 95 then 96 pid=`wait_for_pid mysql 1 1` 97 if test -n "$pid" 98 then 99 echo -e "\033[32m mysql started \033[0m" 100 else 101 $mysql > /dev/null & 102 echo -n "mysql starting" 103 pid=`wait_for_pid mysql 1` 104 if test -n "$pid" 105 then 106 echo -e "\033[32m [ok] \033[0m" 107 else 108 echo -e "\033[31m [fail] \033[0m" 109 fi 110 fi 111 fi 112 if [[ $1 = redis || $1 = all ]] 113 then 114 pid=`wait_for_pid redis 1 1` 115 if test -n "$pid" 116 then 117 echo -e "\033[32m redis started \033[0m" 118 else 119 $redis $redis_conf 120 echo -n "redis starting" 121 pid=`wait_for_pid redis 1` 122 if test -n "$pid" 123 then 124 echo -e "\033[32m [ok] \033[0m" 125 else 126 echo -e "\033[31m [fail] \033[0m" 127 fi 128 fi 129 fi 130 ;; 131 stop) 132 if [[ $1 = php || $1 = all ]] 133 then 134 pid=`wait_for_pid php 1 1` 135 if test $pid 136 then 137 kill -INT $pid 138 fi 139 echo -n "php stoping" 140 pid=`wait_for_pid php 0` 141 if test -z "$pid" 142 then 143 echo -e " \033[32m [ok] \033[0m" 144 else 145 echo -e " \033[31m [ok] \033[0m" 146 fi 147 fi 148 if [[ $1 = nginx || $1 = all ]] 149 then 150 pid=`wait_for_pid nginx 1 1` 151 if test $pid 152 then 153 $nginx -s stop 154 fi 155 echo -n "nginx stoping" 156 pid=`wait_for_pid nginx 0` 157 if test -z "$pid" 158 then 159 echo -e " \033[32m [ok] \033[0m" 160 else 161 echo -e " \033[31m [fail] \033[0m" 162 fi 163 fi 164 if [[ $1 = mysql || $1 = all ]] 165 then 166 pid=`wait_for_pid mysql 1 1` 167 if test $pid 168 then 169 kill $pid 170 fi 171 echo -n "mysql stoping" 172 pid=`wait_for_pid mysql 0` 173 if test -z "$pid" 174 then 175 echo -e " \033[32m [ok] \033[0m" 176 else 177 echo -e " \033[31m [fail] \033[0m" 178 fi 179 fi 180 if [[ $1 = redis || $1 = all ]] 181 then 182 pid=`wait_for_pid redis 1 1` 183 if test $pid 184 then 185 kill $pid 186 fi 187 echo -n "redis stoping" 188 pid=`wait_for_pid redis 0` 189 if test -z "$pid" 190 then 191 echo -e " \033[32m [ok] \033[0m" 192 else 193 echo -e " \033[31m [fail] \033[0m" 194 fi 195 fi 196 197 ;; 198 restart) 199 if [[ $1 = php || $1 = all ]] 200 then 201 pid=`wait_for_pid php 1 1` 202 if test -n "$pid" 203 then 204 kill -USR2 $pid 205 else 206 $php -y $php_conf 2>&1 & 207 fi 208 pid=`wait_for_pid php 1` 209 if test -n "$pid" 210 then 211 echo -e "php restarting\033[32m [ok] \033[0m" 212 fi 213 fi 214 if [[ $1 = nginx || $1 = all ]] 215 then 216 pid=`wait_for_pid nginx 1 1` 217 if test -n "$pid" 218 then 219 $nginx -s reload 220 else 221 $nginx -c $nginx_conf 2>&1 222 fi 223 pid=`wait_for_pid nginx 1` 224 if test -n "$pid" 225 then 226 echo -e "nginx restarting \033[32m [ok] \033[0m" 227 fi 228 fi 229 if [[ $1 = mysql || $1 = all ]] 230 then 231 pid=`wait_for_pid mysql 1 1` 232 if test -n "$pid" 233 then 234 kill -HUP $pid 235 else 236 $mysql > /dev/null & 237 fi 238 echo -n "mysql restarting" 239 pid=`wait_for_pid mysql 1` 240 if [ -n "$pid" ] 241 then 242 echo -e " \033[32m [ok] \033[0m" 243 else 244 echo -e " \033[31m [fail] \033[0m" 245 fi 246 fi 247 if [[ $1 = redis || $1 = all ]] 248 then 249 pid=`wait_for_pid redis 1 1` 250 if test -n "$pid" 251 then 252 kill $pid 253 fi 254 pid=`wait_for_pid redis 0` 255 if test -z "$pid" 256 then 257 $redis $redis_conf 258 fi 259 echo -n "redis restarting" 260 pid=`wait_for_pid redis 1` 261 if [ -n "$pid" ] 262 then 263 echo -e " \033[32m [ok] \033[0m" 264 else 265 echo -e " \033[31m [fail] \033[0m" 266 fi 267 fi 268 269 ;; 270 esac
用法:
/sboot.sh [php|mysql|nginx|redis|all] [start|stop|restart]
学习shell时写的,看着比较low,但还是比较好用吧
浙公网安备 33010602011771号