-
-
Save a-yasui/4323688 to your computer and use it in GitHub Desktop.
MAMP subtool
コマンドラインで DocumentRoot を現在の作業ディレクトリに変更して再起動します # 間違ってanonymousで書いてもうたよ…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# -*- coding: utf_8 -*- | |
# | |
# MAMPcwd (start|stop|cwd) | |
# MAMP のコマンドライン補佐ツール | |
# | |
# start: 再起動/起動をします | |
# stop : 停止します | |
# cwd : コマンドを実行したディレクトリをDocumentRootにして、再起動をします | |
# | |
# 注意 | |
# MAMP 本体を起動してこのコマンドを使い、MAMP 本体を終了した時、httpd は落ちます。 | |
# 正常動作ですが、このことを忘れないでください。 | |
# | |
# Ex: | |
# | |
# [~/Desktop/work/test] $ MAMPcwd cwd | |
# MAMP の DocumentRoot を'/Users/.../Desktop/work/test' にして再起動します | |
# | |
# Log: | |
# | |
# 2013/05/10: resやcwdした時にhttpdが死ぬ時があるので、丁寧に再起動するように改良 | |
MAMP="/Applications/MAMP" | |
if [ ! -d $MAMP ]; then | |
echo "Not found MAMP" | |
exit 1 | |
fi | |
if [ ! -e $MAMP/conf/apache/httpd.conf ]; then | |
echo "Not Found httpd.conf" | |
echo "Reinstall at MAMP" | |
exit 1 | |
fi | |
# ======================================================================================= | |
## Apache 生存確認 | |
# @result integer 0 の時動いておらず、1の時動いてる。 | |
function apache_check () { | |
pidfile="$MAMP/Library/logs/httpd.pid" | |
if [ ! -e "$pidfile" ]; then | |
return 0; | |
fi | |
pid=`cat $pidfile` | |
if [ "x$pid" == "x" ]; then | |
echo "PID is not found $pid"; | |
return 0; | |
fi | |
p=`ps -p $pid | grep httpd` | |
if [ "x$p" != "x" ]; then | |
return 1; | |
fi | |
return 0; | |
} | |
function apache_start () { | |
apache_check | |
chk=$? | |
if [ $chk -eq 1 ]; then | |
echo "Apache Already Running. Stop now"; | |
apache_stop; | |
sleep 3 | |
fi | |
$MAMP/bin/startApache.sh | |
echo "Apache Start Now." | |
} | |
function apache_stop () { | |
apache_check | |
chk=$? | |
if [ $chk -eq 1 ]; then | |
echo "Apache Stop Now"; | |
$MAMP/bin/stopApache.sh | |
fi | |
} | |
function apache () { | |
case $1 in | |
start) apache_start; | |
;; | |
stop) apache_stop; | |
;; | |
status) | |
$MAMP/Library/bin/apachectl configtest | |
;; | |
cwd) | |
echo "MAMP のDocumentRoot を"`pwd`"にして再起動します" | |
apache_stop; | |
cat $MAMP/conf/apache/httpd.conf \ | |
| awk '{gsub(/^DocumentRoot.+/, "DocumentRoot \"'`pwd`'/\"")} {print}' \ | |
| awk '{gsub(/^Listen.+/, "Listen 8888")} {print}' \ | |
| awk '{gsub(/^<Directory "\/U.+">/, "<Directory \"'`pwd`'/\">")}{print}' \ | |
> /tmp/abc.conf; | |
mv /tmp/abc.conf $MAMP/conf/apache/httpd.conf; | |
chown :admin $MAMP/conf/apache/httpd.conf; | |
xattr -w com.apple.TextEncoding utf-8 $MAMP/conf/apache/httpd.conf; | |
apache_start; | |
;; | |
esac | |
} | |
# ======================================================================================= | |
function mysql_chech () { | |
if [ -e $MAMP/tmp/mysql/mysql.sock ]; then | |
return 1 | |
fi | |
return 0 | |
} | |
function mysql_start () { | |
mysql_chech | |
chk=$? | |
if [ $chk -eq 0 ]; then | |
echo "MySQL Start Now"; | |
$MAMP/bin/startMysql.sh | |
fi | |
} | |
function mysql_stop () { | |
mysql_chech | |
chk=$? | |
if [ $chk -eq 1 ]; then | |
echo "MySQL Stop Now"; | |
$MAMP/bin/stopMysql.sh | |
fi | |
} | |
function mysql () { | |
case $1 in | |
start) mysql_start; | |
;; | |
stop) mysql_stop; | |
;; | |
esac | |
} | |
for arg in $*; do | |
case $arg in | |
start) | |
apache stop | |
mysql stop | |
mysql start | |
apache start | |
;; | |
stop) | |
apache stop | |
mysql stop | |
;; | |
res) | |
apache stop | |
mysql stop | |
sleep 5 | |
mysql start | |
apache start | |
;; | |
cwd) | |
apache cwd | |
mysql start | |
;; | |
help|*) | |
echo "MAMPcwd (start|stop|cwd)" | |
echo " MAMP Command line tool" | |
echo "" | |
echo " start: Start up to MAMP" | |
echo " stop : Stop to MAMP" | |
echo " cwd : Change DocumentRoot Path at current work directory" | |
echo " res : Restart Apache and MySQL" | |
;; | |
esac | |
done | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment