3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

zaleniumの起動・停止制御を行うスクリプト

Posted at

自分の環境のみかもしれないけど、Zaleniumとphpunitを長時間稼働、長時間テスト実行しているとメモリ不足でエラーが出まくるので対策として起動・停止を外から管理できるようなbashスクリプトを作ってみた
zaleniumについては以下参照
zaleniumのgithub
現状、zalenium止めたらメモリは復活するから原因の特定はやらない予定
/opt/docker_volume/は共有用の場所

script
#!/bin/bash

# status:
#   stop    : stop zalenium
#   run     : start zalenium
#   running : no touch

function start_zalenium() {
    # 起動してたら止める
    stop_zalenium
    docker pull elgalu/selenium
    docker pull dosel/zalenium
    # start zalenium container
    docker run --rm -i --name zalenium -p 4444:4444 -v /var/run/docker.sock:/var/run/docker.sock -v /opt/docker_volume/zalenium:/home/seluser/videos --privileged dosel/zalenium start --timeZone 'Asia/Tokyo' --debugEnabled true
}

function stop_zalenium() {
    if_value=$(docker ps -a | grep -c "zalenium")
    if [[ "$if_value" != "0" ]]; then
        docker ps -a | grep "zalenium" | awk '{print $1}' | xargs docker stop
    fi
    echo "#---------------#"
    echo "# zalenium stop #"
    echo "#---------------#"
    echo ""
}

function execute() {
    ZALENIUM_SCRIPT_STATUS_FILE="zalenium_script_status"
    touch $ZALENIUM_SCRIPT_STATUS_FILE
    while [[ -f "$ZALENIUM_SCRIPT_STATUS_FILE" ]]; do
        now_status=$(head -n1 $ZALENIUM_SCRIPT_STATUS_FILE)
        if [[ "$now_status" == "run" ]]; then
            start_zalenium &
            echo "running" > $ZALENIUM_SCRIPT_STATUS_FILE
            # 停止まで適当に待つ
            sleep 300
        elif [[ "$now_status" == "stop" ]]; then
            echo "stoped" > $ZALENIUM_SCRIPT_STATUS_FILE
            stop_zalenium
        fi
        sleep 1
    done
}

execute

一番最初はこれを起動しておいて、テスト実行するときのスクリプトにecho "run" > zalenium_script_statusみたいなのを最初に、テスト終了後にecho "stop" > zalenium_script_statusを実行するように仕込めば動くはず
本当にzaleniumが原因なのかは不明だけど、とりあえずこれで土日放置できる。。。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?