I have the following in my bash script:

# catch input for log-data
exec > >(tee -i ${LOG})
exec 2>&1

I want to exclude one command from being written to this log file, but the following doesn't work:

/sbin/runuser -l USER -c 'COMMAND' >/dev/null 2>&1

It always gets logged to the ${LOG} file.  How can I prevent this?

  • (1) I tried what you say you tried, and the output from the distinguished command did not get written anywhere.  Please clarify what happens when you run it; perhaps giving real commands rather than the generic command.  (For example, date seems to write to stdout reliably; for stderr, use cat, cp and ls with invalid arguments.)  (2) Your comment is wrong.  You are catching the output of subsequent commands, not the input.  (3) You should use "$LOG" (or "${LOG}", if you want) rather than ${LOG}. – Scott May 17 at 17:40
  • I have updated, because of exec 2>&1 all errors get logged to the defined file, but i want for /sbin/runuser that the errors don't get logged to the file. – Omexlu May 17 at 17:43

I think i figured it out, my solution is:

Revert the exec command:

exec 1>&2 2>&-

Do the command

Restore the old exec:

exec 2>&1

If there are a better solution, you can tell me :)

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.