简体   繁体   中英

Bash: Append command output and timestamp to file

Normally in my bash scripts I'm used to do some_command >> log.log . This works fine, however how can I append more data like time and command name?

My goal is to have a log like this

2012-01-01 00:00:01 [some_command] => some command output...
2012-01-01 00:01:01 [other_command] => other command output...

The processes should running and writing to the file concurrently.

The final solution, pointed by William Pursell in my case would be:

some_command 2>&1 | perl -ne '$|=1; print localtime. ": [somme_command] $_"' >> /log.log &

I also added 2>&1 to redirect the STDOUT and STDERR to the file and an & on the end to keep the program on background.

Thank you!

An alternative solution using sed would be:

some_command 2>&1 | sed "s/^/`date '+%Y-%m-%d %H:%M:%S'`: [some_command] /" >> log.log &

It works by replacing the beginning of line "character" (^). Might come in handy if you don't want to depend on Perl.

something like this:

(echo -n $(date); echo  -n " ls => ";  ls) >> /tmp/log

however, your command output is multiple lines and it will not have the format above you are showing. you may want to replace the newline in output with some other character with a command like tr or sed in that case.

Given your comments, it seems that you want multiple processes to be writing to the file concurrently, and have a timestamp on each individual line. Something like this might suffice:

some_cmd | perl -ne '$|=1; print localtime . ": [some_cmd] $_"' >> logfile

If you want to massage the format of the date, use POSIX::strftime

some_cmd | perl -MPOSIX -ne 'BEGIN{ $|=1 }
   print strftime( "%Y-%m-%d %H:%M:%S", localtime ) . " [some_cmd] $_"' >> logfile

On Ubuntu:

sudo apt-get install moreutils
echo "cat" | ts
Mar 26 09:43:00 cat

One approach is to use logger(1).

Another might be something like this:

stamp () {
  ( echo -n "`date +%T` "
    "$@" ) >> logfile
}

stamp echo how now brown cow

A better alternative using GNU sed would be:

some_command 2>&1 | sed 'h; s/.*/date "+%Y-%m-%d %H:%M:%S"/e; G; s/\n/ [some_command]: /'

Breaking down how this sed program works:

# store the current line (a.k.a. "pattern space") in the "hold space"
h;
# replace the current line with the date command and execute it
# note the e command is available in GNU sed, but not in some other version
s/.*/date "+%Y-%m-%d %H:%M:%S"/e;
# Append a newline and the contents of the "hold space" to the "pattern space"
G;
# Replace that newline inserted by the G command with whatever text you want
s/\n/ [some_command]: /

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM