Last active
January 27, 2018 20:30
-
-
Save becxer/30fc93bc56c8ab006a17 to your computer and use it in GitHub Desktop.
pydemon ( relaunch when detect modified )
This file contains hidden or 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/python | |
# | |
# use to run python program forever | |
# @author becxer | |
# @email [email protected] | |
# | |
import signal | |
import sys | |
import subprocess | |
import time | |
import os | |
import datetime | |
import json | |
sleep_time = 1 | |
bashCmd = "" | |
now_time = 1000 | |
last_time = 1001 | |
run_count = 0 | |
pydemon_log = '.pydemon.log' | |
pydemon_dat = '.pydemon.dat' | |
dat_obj = {} | |
if len(sys.argv) == 1: | |
print 'usage : ./pydemon "bash command" or your-script.sh' | |
exit() | |
def set_bashCmd(): | |
global bashCmd | |
bashCmd = "" | |
for i in range(len(sys.argv)): | |
if i > 0 : | |
if os.path.isfile(sys.argv[1]) and ('.sh' in sys.argv[1]): | |
f = open(sys.argv[1],"r") | |
bashCmd = f.read() | |
f.close() | |
break | |
bashCmd += sys.argv[i] + " " | |
def set_now_time(path): | |
global now_time | |
dirs = os.listdir(path) | |
for f in dirs: | |
fnow = path+'/'+f | |
if os.path.isdir(fnow) : | |
set_now_time(fnow) | |
elif os.path.getmtime(fnow) > now_time: | |
now_time = os.path.getmtime(fnow) | |
def run_cmd(bashCmd): | |
print str(bashCmd) | |
print "[stdout]----------------------" | |
process = subprocess.Popen(bashCmd, shell=True | |
,stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
res = '' | |
while True: | |
output = process.stdout.readline() | |
if output == '' and process.poll() is not None: | |
break | |
if output: | |
res += output | |
print output.strip() | |
rc = process.poll() | |
output, error = process.communicate() | |
print "[stderr]----------------------" | |
res += error | |
print error | |
if process.returncode != 0 : | |
print "[ERROR] process error" | |
print "pydemon is watching your directory..." | |
return res | |
def load_dat(datpath): | |
if os.path.isfile(datpath): | |
datf = open(datpath,'r') | |
js = json.loads(datf.read()) | |
datf.close() | |
return js | |
else: | |
return {'run_count':0} | |
def save_dat(obj ,datpath): | |
datf = open(datpath,'w') | |
json.dump(obj,datf,indent=4) | |
datf.close() | |
return None | |
while True: | |
if last_time != now_time : | |
set_bashCmd() | |
dat_obj = load_dat(pydemon_dat) | |
dat_obj['run_count'] += 1 | |
print "[#"+str(dat_obj['run_count'])+"]---------------------------" | |
logf = open(pydemon_log,"a") | |
logf.write("T: " +str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) | |
+" #"+str(dat_obj['run_count'])+" Log\n") | |
logf.write(run_cmd(bashCmd)) | |
logf.close() | |
save_dat(dat_obj,pydemon_dat) | |
set_now_time('./') | |
last_time = now_time | |
else : | |
set_now_time('./') | |
time.sleep(sleep_time) |
실시간 으로 프로그램을 관찰할수 있게됨.
로그남기는 기능 추가
카운트 연속 기능 추가
log 에 time stamp 기능 추가
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demon 으로 도는 상태의 어플리 케이션에서도 아웃풋을 찍을수 있으면 좋으련만