LoginSignup
4
5

More than 5 years have passed since last update.

タスクランナー使わない時のローカルサーバー走らせるシェルスクリプトのメモ

Last updated at Posted at 2015-02-05

(追記) 記事にすると色々集約したくなる。もうちょっと整理した。

こんな感じにしてる:

run.sh
#!/bin/bash

CWD=$(pwd)

function ctrl_c () {
    #pgrep -f mongod && pkill mongod  # MongoDB の場合
    pgrep -f mysql && mysql.server stop  # MySQL の場合

    cd ${CWD}                                                                  
    unset CWD
    unset -f ctrl_c
}

trap ctrl_c SIGINT

#mongod --config $(brew --prefix)/etc/mongod.conf &  # MongoDB の場合
mysql.server start  # MySQL の場合

# スクリプトの第 1 引数に HTTP サーバーのルートパスをオプションで指定可能にする
[[ ! -z "${1}" && -d ${1} ]] && cd ${1}

#python -m SimpleHTTPServer 8080  # Python の場合
#ruby -run -e httpd . -p 8080  # Ruby の場合
php -S 0.0.0.0:8080  # PHP の場合

これに実行権限付ける:

chmod +x ./run.sh

そんでローカルサーバー走らせるディレクトリに置いてバックグラウンド実行:

./run.sh &

終了する時はフォアグラウンド化して <C-c> で終了:

Mac:htdocs Genius $ jobs
[1]+  Running                 ./run.sh &
Mac:htdocs Genius $ fg %1
^C
Shutting down MySQL
.. SUCCESS!

HTTP サーバーは適時、必要な環境に置き換えると良いかも:

python -m SimpleHTTPServer 8080
ruby -run -e httpd . -p 8080
php -S 0.0.0.0:8080
4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5