4

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?

4

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 use sed like so:

command 2> /dev/null | sed -e "s/^/$(date) /" >> logfile 

sed -e "s/^/$(date) /" will append the current date in this format Thu 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:

  • To redirect both error and output to a file with time and date, use this:
command 2>&1 | sed -e "s/^/$(date) /" >> logfile
  • To use timestamp in seconds since the Unix epoch like this 1586406740. change $(date) to $(date +%s)

Best of luck

| improve this answer | |
  • Note that here the timestamp will be the time the command is invoked, not the time the output is generated, which could be a huge difference for long-running processes. – user000001 Apr 9 at 14:17
  • This almost worked for me, except that it overwrites the contents of the log file every time I run the script. The time difference you mentioned is not significant in my use case. – user4002112 Apr 9 at 15:43
  • 1
    @user4002112 ` > logfile` will overwrite the log file, but >> logfile will append to the log file. Please make sure to use >> and not > – Raffa Apr 9 at 15:59
  • no I am using >> – user4002112 Apr 9 at 16:57
  • 1
    @Raffa, thanks, it worked for me, I was overwriting in an earlier command. – user4002112 Apr 9 at 21:53
7

It seems you want:

command 2> /dev/null | my_func 

which redirects the stderr of command to /dev/null, but pipes stdout to my_func. (Side note: You might want to check out the ts command).

| improve this answer | |
  • Assuming your my_func is doing the task of writing to the file, command 2>&1 | my_func or (bash-specific) command |& my_func, which redirects stderr to stdout, and stdout is already piped to my_func. – muru Apr 9 at 5:15
  • If your my_func isn't writing to the file, then command 2>&1 | my_func > some/file – muru Apr 9 at 5:15
3

If a timestamp is all that is needed see ts from the moreutils package.

command | ts

$ sudo apt install moreutils
| improve this answer | |
  • 1
    something like this command 2>&1 | ts >> command.log – bac0n Apr 9 at 5:27
1

Another option is to use GNU awk:

myprogram | gawk '{print strftime(), $0}' >> logfile

If you also want to log standard error (assumes bash shell):

myprogram |& gawk '{print strftime(), $0}' >> logfile
| improve this answer | |

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.