LoginSignup
8
8

More than 5 years have passed since last update.

PythonのSimpleHTTPServerをアクセスログをトリガにして終了する

Last updated at Posted at 2014-05-21

まずつまずいたのが、pythonのアクセスログ取得。

python -m SimpleHTTPServer > httpd.log

python -m SimpleHTTPServer 2>&1 > httpd.log

だとうまくいかないが、

python -m SimpleHTTPServer 2>&1 |tee httpd.log

だととれるらしい。

パイプを使用したので、$!ではpythonのPIDが取得できなくなってしまった。そこで
http://qiita.com/mattintosh4/items/35e184d890c4453a8da7
を参考にしながらjobsからPIDを取得した。

PYTHON_PID=`jobs -l|grep +|awk '{print $2}'`

最後に、ログに含まれる文字列から終了を判断したいので、
ログに特定の文字列が含まれないことを確認することにした。

while [ "`cat httpd.log|grep filename.html`" = ""]; do
sleep 1
done

全ソース

python -m SimpleHTTPServer 2>&1 |tee httpd.log
PYTHON_PID=`jobs -l|grep +|awk '{print $2}'`

while [ "`cat httpd.log|grep filename.html`" = ""]; do
sleep 1
done

kill $PYTHON_PID
8
8
2

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
8
8