I have the command in a script:

exec su -s /bin/sh -c 'exec "$0" "$@"  | ts "[%Y-%m-%d %H:%M:%S]"' user -- test.python $FULLPATH   &> log & 

Where FULLPATH is the path of the test Python file. With this command I expected the output of the script to be written into the log file with timestamps from the ts command.

That does actually not happen, the output is written without any timestamps, other than that i also tried to do this:

exec su -s /bin/sh -c 'exec "$0" "$@"' user -- test.python $FULLPATH  | ts "[%Y-%m-%d %H:%M:%S]" &> log & 

This also gives the same result with the previous command.

Any thoughts on why this happens and a possible solution would be much appreciated, thank you.

  • I didn't write the script i just wanted to modify it in order to add the time stamps to the log file. – user3224454 May 8 '17 at 17:48
  • That's an amusing construction. But it seems to work. @KamilMaciorowski, the first part of a pipe runs in a subshell, so exec there just replaces that subshell. Not that it probably matters much, but the pipe will still be set up. – ilkkachu May 8 '17 at 20:52

Using exec with & makes little sense (though that is not going to be the source of your problem), that will not save any extra fork. With pipe components, I can only see it saving a fork with the zsh shell but only if applied on the last pipe component (cmd1 | exec cmd2, and that means zsh no longer waits for the other pipe components).

Here, you're not redirecting the python script stderr to ts, though you are sending it to the log file. You'd probably want:

su -s /bin/sh -c '
  "$0" "$@" 2>&1 |
    ts "[%Y-%m-%d %H:%M:%S]"
  ' user -- test.python "$FULLPATH" > log 2>&1 & 

Lose the nonsense execs, and the part where you are passing half the command after the --:

su -s /bin/sh -c "test.python $FULLPATH | ts '[%Y-%m-%d %H:%M:%S]'" &> log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.