守护进程输出未写入TXT

时间:2016-02-10 09:06:15

标签: python linux ubuntu

我有一个从守护程序服务器调用函数的脚本。守护程序服务器每次完成其作业时都会生成一个输出。守护程序主要从外部服务器下载文件。守护程序源代码已关闭(由外部公司编码)。

完成下载文件后的守护程序输出示例:

Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

脚本我使用来自TXT文件的下载ID,来自用户字段的PC用户名,并将它们放入一个数组然后从守护进程中调用它们获取文件然后写一个日志在txt文件中。

日志样本应为:

Documnent ID= 3
From User = DomainCon
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

我面临的问题是保存到/home/ubuntu/Daemon/downloads/download_1024000003.pdf 每次下载完成后守护程序服务器生成的内容有时在TXT中丢失。

问题示例:

Documnent ID= 2
From User = DomainCon
Documnent ID= 3
In Chat = DomainCon
ANSWER 78
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

日志文件中的输出应

Documnent ID= 2
From User = DomainCon
ANSWER 96
Saved to /home/ubuntu/Daemon/downloads/download_1024000002.pdf
Documnent ID= 3
In Chat = DomainCon
ANSWER 78
Saved to /home/ubuntu/Daemon/downloads/download_1024000003.pdf

这是我使用的脚本,包括评论:

for i in range(len(ids)):\\ ids is the array that contains the documents that should be downloaded.
       cmdping = "sleep 5; echo load_document "+ids[i][0]+"| nc -w 4 127.0.0.1 1234 | tee >> "+logtxt \\ Perpare the Echo  command to the daemon to start the download tee to save the output of the daemon into the log text
       print("\033[1;31m~~~Documnent~~~NB~~~"+str(i+1)+"\033[1;m")
       print ("Documnent ID= "+ids[i][0])\\ Just Print on screen for Debbuging Purpose
       print ("From User= "+ids[i][1])\\Just Print on screen for Debbuging Purpose
       logfile = open(logtxt, "a") \\ Open the TXT where the log is gonna be saved.
       logfile.write("Documnent ID= "+ids[i][0]+"\n")\\Write the Document ID in the File.
       logfile.write("From User = "+ids[i][1]+"\n")\\\\Write the From User in the File.
       logfile.close()\\Close the logfile
       if (i==len(ids)-1):
          p=subprocess.Popen(cmdping, shell=True, stderr=subprocess.PIPE)\\ Start the proccess that was prepared before.
          #time.sleep(1)
          check=""
          s=1
          skip=0
          while (s==1):
              check=checkfile(srcdir)
              if (check!="no"):
                 s=2
              if(check=="skip"):
                 skip=1
          if(skip!=1):
             if(checksize(srcdir+check) == "done"):
                print(check+"\n \033[1;32mFinished Downloading moving to the next download\033[1;m")
          p.terminate()
          user_dir=rootdir+"/document/"+ids[i][1];
          checkandcopy(download,user_dir)

1 个答案:

答案 0 :(得分:1)

您可以考虑使用套接字。这里有一些灵感:

import re
import socket
for i, (doc_id, user) in enumerate(ids):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print("\033[1;31m~~~Documnent~~~NB~~~"+str(i+1)+"\033[1;m")
    s.connect(('127.0.0.1', 1234))
    s.sendall('load_document {}\n'.format(doc_id))
    buf = []
    while True:
        r = s.recv(4096).strip()
        print('Got {} for socket'.format(r))
        buf.append(r)
        if r.endswith('.pdf'):
            print('Done')
            break
    result = '\n'.join(buf)
    s.close()
    with open(logtxt, 'a') as f:
        f.write('Document ID= {}\n'.format(doc_id))
        f.write('From User = {}\n'.format(user))
        f.write('{}\n'.format(result))
    filename = re.search('Saved to (.+)$', result).group(1)
    checkandcopy(filename, userdir)