5
7

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.

GunicornをFabric経由で操作する

Posted at

Gunicornで動いているアプリをサーバー側で起動、再起動ができるようにした。
プロセスを止めるにはpidを指定してkillすればいいということを学んだ。

####gunicorn.conf.py

usr/bin/python
gunicorn.conf.py

bind = "0.0.0.0:5000"
workers = 2
worker_class = 'sync'
max_requests = 1000
timeout = 30
keep_alive = 2
preload = True
daemon = True

####start.sh

GUNICORN=/usr/bin/gunicorn
ROOT=/your/app/path
PID=/var/run/gunicorn/your.pid
APP=run:app

if [ -f $PID ]; then rm $PID; fi
    cd $ROOT
    source venv/bin/activate
    exec $GUNICORN -c $ROOT/gunicorn.conf.py –pid=$PID $APP

####fabfile.py

#coding: utf-8
from fabric.api import run,env,local,settings
from fabric.operations import sudo
from fabric.context_managers import cd
import os
import subprocess

from fabric.api import env, run
env.use_ssh_config = True
env.hosts = ["your ip address"]
env.key_filename = "/root/.ssh/authorized_keys"
env.user = "username"
env.password = "password"


def start():
    with settings():
        with cd("/your/path"):
            sudo("""
            source start.sh
            """,pty=False)

def stop():
    with settings():
        pid = get_pid()
        sudo("kill {}".format(pid))

def restart():
    try:
        stop()
    except:
        print("There isn't pid")
    start()

これで fab start/stop/restartで自由に再起動を行うことができる。

###メモ

  • タスクをバックグラウンドで実行させたい時は pty=False としないと動作しない。

この記事はhttp://furodrive.com/ja/2014/2/stop_and_start_gunicorn
を元に作成されました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?