2

How do I get the output of this python script to append to a log file

sudo inotifywait -mr --format '%e%w%f' --event move $srdir | python3 /usr/bin/m-sync.py $bkdir $srdir >> /mnt/msync.py.log2 doesn't work. No error message, just an empty un-modified log file.

If I do just sudo inotifywait -mr --format '%e%w%f' --event move $srdir | python3 /usr/bin/m-sync.py $bkdir $srdir the output spits out to the console as expected.

Ultimately sudo inotifywait -mr --format '%e%w%f' --event move $srdir | python3 /usr/bin/m-sync.py $bkdir $srdir >> /mnt/msync.py.log2 is runs from a script as a service by systemd, but I don't think that's relevant since when I run it from the command line manually it still doesn't log.

In all cases the script actually does it's job correctly, I just can't get it's output into a log.

The python script just incase it matters:

#!/usr/bin/python3
import sys
import time
from subprocess import call
src = ""
bkdir = sys.argv[1]
srdir = sys.argv[2]
while True:
        line = sys.stdin.readline()
        event, path = line.split("/",1)
        path="/"+path.rstrip();
        if event in  ["MOVED_FROM","MOVED_FROM,ISDIR"]:
                src = path.replace(srdir, bkdir, 1)
        if event in ["MOVED_TO","MOVED_TO,ISDIR"]:
                dst  =  path.replace(srdir, bkdir, 1)                    
                sys.stdout.write(time.strftime("%Y-%m-%d %H:%M:%S") + " mv " + src + " " + dst )
                call(["mv", src, dst])

Edit: Apparently sudo inotifywait -mr --format '%e%w%f' --event move $srdir | sudo python3 /usr/bin/m-sync.py $bkdir $srdir >> /mnt/msync.py.log2 run from the command line does work. Notice the second sudo before invoking python. However when run by systemd everything is root by default and still doesn't output to the log.

3
  • It is always advantageous to fill in as many details as possible, instead of just saying "it doesn't work". If you don't get any error message, mention that too. In this case I suspect the fault lies in that only the first part of the pipeline is run as root, i.e. the sudo only applies to the inotifywait command, not to what's after the pipe symbol. Nov 21, 2016 at 11:29
  • Do you not get output in the log while the script is running, or even after the script finishes do you still have only an empty file? Nov 21, 2016 at 14:34
  • @Johan Myréen Ah! You are right in the case of me running the it from the command line. sudo inotifywait -mr --format '%e%w%f' --event move $srdir | sudo python3 /usr/bin/m-sync.py $bkdir $srdir >> /mnt/msync.py.log2 does update the log. However when run by systemd everything already was running as root and still didn't update it.
    – Geo R
    Nov 21, 2016 at 14:39

1 Answer 1

2

From https://stackoverflow.com/a/3167526/7192818

I don't know if this applies to python as well, but I think it depends on the operating system that you are running.

On Linux for example, output to terminal flushes the buffer on a newline, whereas for output to files it only flushes when the buffer is full (by default). This is because it is more efficient to flush the buffer fewer times, and the user is less likely to notice if the output is not flushed on a newline in a file.

You might be able to auto-flush the output if that is what you need.

So in the python script adding sys.stdout.flush() after the write makes it behave as expected.

Meh.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .