I can use 2> errorLogFile
to log error of running script to a file right? my question is how to log the date and time of error along with error?
for example:
$ cat myScript
2> errorLogFile
..... # rest of the script
this is how the error file should look like:
$ cat errorLogFile
21 Nov 2015 2:00 PM some error happened
You could redirect
stderr
to a process substitution which processesstderr
and outputs the processed lines to the log.For example, this script will print 5 lines to
stdout
and 5 lines tostderr
, redirectingstderr
to a process susbstitution which processesstderr
and outputs the processed lines to a file named "log" in the current working directory (mind that the syntax to redirectstderr
isexec 2> [...]
, not2> [...]
):The process substitution invokes Perl, which appends the current date and time in that format to each line and outputs them to a file named "log" in the current working directory.