Fix bug where command isn't printed until after output

'print' was previously used to print the command being executed by _exec.
Due to buffering, the printed command wasn't written to the output file until
after the command was executed. Changed to an explicit write() and flush()
of the file so that it is written before the program output