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.
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.