Last active
December 19, 2015 17:48
-
-
Save georgefs/5993587 to your computer and use it in GitHub Desktop.
管裡開啟uwsgi.ini 的/etc/init.d
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
#! /usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright © 2013 george | |
# | |
# Distributed under terms of the MIT license. | |
__all__=[] | |
import os | |
import sys | |
import re | |
PATH = "/etc/uwsgi/" | |
def get_inits(): | |
for filename in os.listdir(PATH): | |
if filename.endswith('.ini'): | |
yield "{}{}".format(PATH, filename) | |
def get_all(): | |
for path in get_inits(): | |
yield UWsgi_Maker.parse_name(path) | |
class UWsgi_Maker(object): | |
pid_path="/tmp/" | |
log_path="/tmp/" | |
def __init__(self, name): | |
self.name = name | |
self.path = UWsgi_Maker.get_path(name) | |
@property | |
def pid_file(self): | |
return os.path.join(self.pid_path, self.name+".pid") | |
@property | |
def log_file(self): | |
return os.path.join(self.log_path, self.name+".log") | |
@classmethod | |
def parse_name(cls, path): | |
return re.search('[^/]*(?=.ini$)', path).group() | |
@classmethod | |
def get_path(cls, name): | |
return os.path.join(PATH, name+'.ini') | |
def stop(self): | |
try: | |
with open(self.pid_file) as pid_file: | |
pids = pid_file.read().split('\n') | |
for pid in pids: | |
if not pid: | |
continue | |
cmd = 'kill -9 {}'.format(pid) | |
i, o= os.popen4(cmd) | |
print o.read() | |
os.remove(self.pid_file) | |
except Exception as e: | |
print '{} not start'.format(self.name) | |
def start(self): | |
cmd = "uwsgi --ini {} --daemonize={} --pidfile={}".format(self.path, self.log_file, self.pid_file) | |
i, o = os.popen4(cmd) | |
print o.read() | |
def restart(self): | |
self.stop() | |
self.start() | |
if __name__ == '__main__': | |
argv = sys.argv | |
if len(argv) == 1: | |
raise 'need command' | |
cmd = argv[1] | |
if len(argv) == 2: | |
names = get_all() | |
else: | |
names = argv[2:] | |
for name in names: | |
try: | |
print "{} with {}".format(cmd, name) | |
uwsgi_makers = UWsgi_Maker(name) | |
getattr(uwsgi_makers, cmd)() | |
except Exception as e: | |
print "{} error {}".format(name, e) | |
finally: | |
print '\n' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment