django 调用 shell 脚本,日志实时返回问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fanne
V2EX    Django

django 调用 shell 脚本,日志实时返回问题

  •  
  •   fanne 2017 年 4 月 7 日 7033 点击
    这是一个创建于 3209 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码内容:

    from subprocess import PIPE,Popen,STDOUT @celery_app.task def call_shell(ipList,add_ipList,shell_log,shell_dir,shell_file): os.popen(r"echo %s >> %s" % (ipList, add_ipList)) # output = os.popen("cd %s;/bin/sh %s" % (shell_dir, shell_file)) p = Popen("cd %s;/bin/sh %s" % (shell_dir, shell_file),stdout=PIPE,stderr=STDOUT,shell=True) for line in iter(p.stdout.readline,""): shell_log.write(line) shell_log.flush() shell_log.close() os.popen(r"/bin/sed -i '$d' %s" % add_ipList) 

    在这里我用了 subprocess.Popen 去执行一个 shell 脚本,然后 shell 执行的时候会有一大串内容输出,我直接用了p.stdout.readline读取它到一个 txt 文件中,这个文件我弄成了一个 url 去访问他: http://192.168.1.21/shell_log.txt

    最后的问题在于,只有当 shell 内容全部读取后,我才能在页面刷新出内容的。

    有没有什么办法, shell 输出一点,我就能在 http://192.168.1.21/shell_log.txt 访问到一点的。

    就是有点类似需要实时的看到 shell 日志输出的。而不是当整个 shell 输出完, p.stdout.readline 全部读取完后才能在页面看到的。

    10 条回复    2017-04-13 17:22:58 +08:00
    0asis
        1
    0asis  
       2017 年 4 月 7 日 via iPhone
    之前做过用 redis 存放内容然后 view 从 redis 里边取
    okletswin
        2
    okletswin  
       2017 年 4 月 7 日
    class RunningProcess(object):

    def __init__(self, process):
    self.process = process

    def is_running(self):
    return bool(self.process.poll() is None)

    def readline(self):
    line = self.process.stdout.readline()
    return line

    @property
    def unread_lines(self):
    lines = self.process.stdout.readlines()
    self.process.stdout.close()
    return lines

    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT, universal_newlines=True)

    rp = RunningProcess(p)

    while rp.is_running():
    line = rp.readline()
    print(line)

    for line in p.unread_lines:
    print(line)
    okletswin
        3
    okletswin  
       2017 年 4 月 7 日
    额,不知道怎么格式化,将就看吧。。。
    fanne
        4
    fanne  
    OP
       2017 年 4 月 7 日
    @okletswin 回复貌似也是可以用 markdown 语法
    ```python
    class RunningProcess(object):

    def __init__(self, process):
    self.process = process

    def is_running(self):
    return bool(self.process.poll() is None)

    def readline(self):
    line = self.process.stdout.readline()
    return line

    @property
    def unread_lines(self):
    lines = self.process.stdout.readlines()
    self.process.stdout.close()
    return lines
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT, universal_newlines=True)
    rp = RunningProcess(p)
    while rp.is_running():
    line = rp.readline()
    print(line)
    for line in p.unread_lines:
    print(line)
    ```
    这样的么
    fanne
        5
    fanne  
    OP
       2017 年 4 月 7 日
    额,好像也不行。
    fanne
        6
    fanne  
    OP
       2017 年 4 月 8 日
    稍微给了一点,按照这样就可以了,一边读取日志,未读取完,刷新页面也可以看到日志信息了。
    @celery_app.task
    def call_shell(ipList_item):
    add_ipList = '/qzdata1/ansible_ops/shellDir/ipList'
    shell_dir = '/qzdata1/ansible_ops/shellDir'
    shell_file = 'v3_addServer.sh'
    nowTime = time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime(time.time()))
    shell_log = 'static/searchlog/add_game_log_%s.txt' %nowTime
    myfile = open(shell_log,"w",0)
    os.popen("echo %s >> %s" %(ipList_item,add_ipList))
    p = Popen("cd %s && /bin/sh %s" %(shell_dir,shell_file),stdout=PIPE,stderr=STDOUT,shell=True)
    for line in iter(p.stdout.readline,""):
    myfile.write(line)
    myfile.close()
    os.popen(r"/bin/sed -i '$d' %s" %add_ipList)
    Wellido
        7
    Wellido  
       2017 年 4 月 13 日
    请问博主是调用本地的 shell 脚本吗?
    fanne
        8
    fanne  
    OP
       2017 年 4 月 13 日
    @Wellido 嗯,是的。
    kba977
        9
    kba977  
       2017 年 4 月 13 日
    楼主你好,请问可以给我你的邮箱或者微信么,我也有类似的需求,卡了一段时间了。想请教一下。
    fanne
        10
    fanne  
    OP
       2017 年 4 月 13 日
    @kba977 QQ:896661380
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5239 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 44ms UTC 09:23 PVG 17:23 LAX 01:23 JFK 04:23
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86