I am writing a backup script that I am going to execute in cronjob every night.
The script sets sys.stdout and sys.stderr to the output file to keep an event log.
For backup I use the following code
cmd = 'rsync -av --del --stats --filter "- .thumbnails/" ' + \ '--filter "- *~" --filter "- *.iso" --filter "- lost+found/" ' + \ '--filter "- .cache/" --filter "- tmp/" --filter "- *.mp3" ' + \ '--filter "- *.log" ' + srcDir + ' ' + dstDir print "Executing '"+cmd+"' ..." try: sys.stdout.flush() sys.stderr.flush() retcode = subprocess.call( cmd, stdin = sys.stdin, stdout = sys.stdout, stderr=sys.stderr, shell=False ) if retcode < 0: print >>sys.stderr, "Command was terminated by signal", -retcode elif retcode > 0: print >>sys.stderr, "Command returned code ", retcode except OSError, e: print >>sys.stderr, "Execution failed:", e
I add print instructions before and after the subprocess call. The problem is that I get the subprocess call output before any output of my print instructions before the call. I have added flush () calls, but this has no effect.
Why is this happening and how can I change this behavior?
python file
chmike
source share