1

I have a simple bash script that launches an executable in the background and redirects stdout + stderr to a log file:

#!/usr/bin/bash
myexec >& logfile &

It works. However, output from myexec isn't the only thing that gets redirected: any messages that bash emits while attempting to invoke myexec are also going to logfile. To wit, if bash doesn't find myexec, I don't get to see the myexec: No such file or directory error because it went straight to logfile instead of to the terminal. This behavior annoys me because I end up not knowing whether the script succeeded in starting up myexec.

It occurs to me that the script could just test for the existence of myexec before trying to invoke it, but I'm wondering whether there isn't a way to do the redirection itself in such a way that only myexec's output, and not the shell's, gets redirected.

3
  • No, there isn't really, though perhaps you could try some of the answers in this near-duplicate: stackoverflow.com/questions/21926647/…
    – tripleee
    Commented Jan 7, 2020 at 19:08
  • That No such file or directory comes back from the execve() syscall, which takes place after the fork() and all the file descriptor redirection already happened. I don't see an obvious way to implement a shell that doesn't have this behavior, at least without creating a hard dependency on an operating system having O_CLOEXEC to maintain a separate mechanism for passing errors back over a FIFO that gets automatically closed on a successful exec. Commented Jan 7, 2020 at 22:14
  • Thank you both. @charles-duffy, your explanation makes it clear why it behaves this way. @tripleee, from your link I see that there exist hacks that are not overly ugly. I've settled on the form myexec > logfile 2>&1 && echo "ok" >&2 || echo "nope." >&2.
    – 76 Pinto
    Commented Jan 8, 2020 at 20:50

1 Answer 1

1

It's not possible to separate the outputs in the way the OP describes. As Charles Duffy explains in his comment, the system call that opens (or fails to open) the executable myexec takes place after Bash has forked a new process, at which point all of the I/O redirection has already been set up. There is, however, a workaround that suffices for the purpose stated in the OP, namely, "knowing whether the script succeeded in starting up myexec":

myexec > logfile 2>&1 && echo "ok" >&2 || echo "nope." >&2
1
  • The only problem here is that it treats any nonzero exit status from myexec the same way as an inability to fork. As you say, though, this limit is pretty much unavoidable with UNIX being designed as it is -- part of why systemd has Type=forking (so a failure before the process forks is treated as the process not having started successfully, whereas the forked-off process eventually exiting with a nonzero status is treated as a successful start and then a later failure) and Type=notify (where the process is responsible for writing updates on its current status over a socket). Commented Jan 8, 2020 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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