0

I have deployed a post-receive hook script and I want to get whole output logs in a file as a block starting with date. I have tried one method but it take line by line log, meaning I have to put that command against every line to get the log. Is there any way to get the whole script log once starting with date?

Script file looks like this:

#!/bin/bash any_command | ts '[%F %T]' >> /home/Man/Man_log.log second_command | ts '[%F %T]' >> /home/Man/Man_log.log third_command | ts '[%F %T]' >> /home/Man/Man_log.log 

see i have to put this line | ts '[%F %T]' >> /home/Man/Man_log.log against every command to get log. And I have 90 lines, so it is not a perfect way to do this. I need an efficient way, like only one line in my script which takes the output of the whole script as log and stores it to another Man_log.log file starting with the date.

What i want is similar to this.

#!/bin/bash ts '[%F %T]' >> /home/Man/Man_log.log #a command which can store logs of every command below this to a separate file starting with date any_command second_command third_command 
2
  • What is that last monospaced block? Is it desired output - if so, why does it start with a command? Commented May 4, 2018 at 17:19
  • I have edited my question. Commented May 5, 2018 at 14:29

2 Answers 2

1

Probably the easiest way would be to modify your script to print the date everytime is called:

#!/bin/sh date -u +"%Y-%m-%dT%H:%M:%SZ" # your commands below ... 

if you can't modify the script then you could group your commands for example:

(date && anycommand) >> out.log 

Grouping a (list of commands) in parenthesis causes them to be executed as if they were a single unit. "sub-shell"

Sign up to request clarification or add additional context in comments.

3 Comments

No actually i don't want to group the whole commands in parenthesis.
I have use this technique and it works like charm (serverfault.com/questions/103501/…) but now their is one issue it overwrite the log file means everytime i run a script it remove the previous log and add new.
@Pasha the >> should append instead of overwriting or you could use | tee -a out.log the -a will append
0

Let's say this is the script that should give us some logs:

#!/bin/bash echo "log_line1 log_line2 log_line3 log_line4" 

And let's call it script.sh.

Running it as ./script.sh | printf "%(%F)T {\n$(cat)\n}" >> Man_log.log, content of Man_log.log will be:

2018-05-04 { log_line1 log_line2 log_line3 log_line4 } 

Let me now explain what exactly the pipe does.

  • %(%F)T is replaced with current date in YEAR-MONTH-DAY format
  • $(cat) are the logs that are output of ./script.sh. In general it is data received as standard input. Basically ./script.sh writes it's logs to standard output. The pipe then passes that standard output to standard input of echo. Running cat without parameters is the same as cat /dev/stdin.

6 Comments

It would be more efficient to use printf with the %(format)T modifier (bash 4.2 or later) rather than echo with the date external program.
@cdarke thanks for the feedback. I updated the answer with your proposal.
Appreciate your suggestions. Do i write this whole command every time in a shell ./script.sh | printf "%(%F)T {\n$(cat)\n}" >> Man_log.log
Well, yes. But if you want this kind of logging every single time you run the script this it's kinda boring. If you don't want to change output of your script you can keep printf "%(%F)T {\n$(cat)\n}" as separate script e.g. wrap_with_date.sh so you can call your script with ./script.sh | ./wrap_with_date.sh >> Man_log.log.
@dmadic i have edited my question hope now you get it what actually i want.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.