Situation: I have a shell script running. Whenever a file appears in /etc/scripts, the shell script will chmod it to be executable, and then run it. It will then redirect it's error and output into 2 other files.
Example: 1000.run appears. Shell script takes 1000.run, makes it executable, runs it, and redirects it's output into 1000.out and 1000.err:
chmod +x 1000.run
sudo -u pi ./1000.run 1>1000.out 2>1000.err
After 1000.out and 1000.err have appeared, I have another script that is watching for these files and then reads out the output and the error.
However, I have a problem: longer commands. Take the following contents of 1000.run:
sleep 30 && ls
Immediately after running ./1000.run, 1000.out and 1000.err appears - both empty. My other script takes those files and says "hey, we have output, the command is done" and returns to me empty outputs (when I was expecting ls output).
In reality, after 30 seconds, output from ls does appear, but at that point my program has already read in the out and err files and concluded that no output was actually received.
Question: Is there a way to delay the creation of the redirection files (1000.out and 1000.err) until the entire command is done running?
What I have tried so far:
 
     
    