简体   繁体   中英

Python script in infinite loop

Script is not exiting after file creation.

If I use below status with 1 string script works fine and quits after install.txt creation, But while reading logs with tail -f script never quits after install.txt file creation and continue reading logs. Even re.findall() is not working with reading log. I don't understand what is the difference and what I am doing wrong?

import sys
import os
import re 
import os.path


def getDownloadStatus():
    #status = "2019-09-16 8:52:05.924|main|INFO|CLIENT|Downloading Fileset: Adobe_Creative_Cloud_Desktop - TEST ID: 12273434579618"
    status = os.system('tail -f /var/log/fwcld.log | grep "Downloading Fileset:"')
    res = re.findall(r'\w+', status) 
    say = "Fileset"
    if say in res:
        split = res.index(say)  
        after = res[split+1:]
        x=' '.join(after)
    if(os.path.isfile('/var/log/install.txt')):
        return "Installations complete"
    else:
        return (x)

#Script
while not getDownloadStatus() == "Installations complete":
    newMES = getDownloadStatus()
    print(newMES)
    os.system('shutdown -r now')

First, the result of os.system on Unix is a status rather than a string you can search; only on Windows does it look like it may be the string resulting from the stdout of the executed command (being OS-dependent is a red flag that you're not using the best tool here – see the subprocess module).

Secondly, the point of the -f flag to tail is to tell it not to exit, but (in this case) to keep the pipe open awaiting further lines written to the end of /var/log/fwcld.log . Thus, that tail process won't terminate until you kill it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM