I need to redirect the output of a command that I am running to a log file with timestamp (hence a function instead of the log file itself) but the error and the output, both should not be showing in the console. The following –
command | my_func 2> /dev/null
is not working.
Something like
command 2> logfile
works, but I can't record the timestamp in the second case. What should I do?
Best Answer
You do not even need a function for that!
To redirect errors to
/dev/null
and output to a file with time and date, you could usesed
like so:sed -e "s/^/$(date) /"
will append the current date in this formatThu 09 Apr 2020 07:06:14 AM +03
to the beginning of each line and add a space between the date and the output for readability purposes.Notice:
1586406740
. change$(date)
to$(date +%s)
Best of luck