Bash snippet - Run a command, see & log its output and execution time.


SUBMITTED BY: DanielK

DATE: Aug. 13, 2016, 3:08 p.m.

FORMAT: Bash

SIZE: 343 Bytes

HITS: 792

  1. function runl() {
  2. if [ $# -gt 1 ]; then
  3. CMDLINE=${@:1:$#-1};
  4. DATE=$(date '+%T %B %d %Y');
  5. LOGFILE=${@:(-1)};
  6. ( echo -e 'DATE: '${DATE}'\n''CMDLINE: '${CMDLINE}'\n' | tee "${LOGFILE}"
  7. (eval time "${CMDLINE}") |& tee -a "${LOGFILE}" && exit $PIPESTATUS )
  8. else
  9. echo "${FUNCNAME} <command> <logfile>"
  10. fi
  11. }

comments powered by Disqus