0

Most logs on my Mac have a time stamp before the output message like this:

Nov 14 17:55:24 - SoftRAID Driver: SoftRAID driver loaded, version 5.8.1.

I am running an executable in cron and instead of using mail I want to output all info to a log file and add a timestamp to each line in front of the output. I am using >> to append to a single file.

This is my crontab:

SHELL=/bin/bash
MAILTO=""
* 6-23 * * * /usr/local/bin/urlwatch >> /Users/john/cronjobs/urlwatch.log

This is what I get in urlwatch.log

UNCHANGED: (01)urlwatch update released (https://github.com/thp/urlwatch/releases/latest)

How do I make the output look like this:

Nov 14 17:55:24 - UNCHANGED: (01)urlwatch update released (https://github.com/thp/urlwatch/releases/latest)

I have tried many different suggestions from across the web and have had no luck. If saving to a text file is easier that will work as well.

Tried this which is close:

* 6-23 * * * (date && /usr/local/bin/urlwatch)  >> /Users/john/cronjobs/urlwatch.log

Output in the log file looks like this:

Sun Mar 15 13:35:00 CDT 2020
UNCHANGED: (03)RansomWhere? Objective-See (https://objective-see.com/products/ransomwhere.html)
UNCHANGED: (01)urlwatch update released (https://github.com/thp/urlwatch/releases/latest)
UNCHANGED: (02)urlwatch webpage (https://thp.io/2008/urlwatch/)
John
  • 145
  • Does your system provide the ts command? – steeldriver Mar 15 '20 at 19:07
  • I have urlwatch 2.17 and unchanged: true in $HOME/.urlwatch/urlwatch.yaml and every time I run urlwatch it shows whole HTML code of the page and 2x UNCHANGED lines: 01. UNCHANGED: https://github.com/thp/urlwatch/releases/latest and UNCHANGED: https://github.com/thp/urlwatch/releases/latest. Do you mean you always get a single line that says UNCHANGED: (01)urlwatch update released (https://github.com/thp/urlwatch/releases/latest)? – Arkadiusz Drabczyk Mar 15 '20 at 19:09
  • @steeldriver "-bash: ts: command not found". I could add via homebrew saw this on AppleStackExchange: "Try brew install moreutils and then run the ts command." – John Mar 15 '20 at 19:20
  • @steeldriver ts installed. What do you suggest? – John Mar 15 '20 at 19:27
  • ... so you should be able to do something like /usr/local/bin/urlwatch | /path/to/ts >> /Users/john/cronjobs/urlwatch.log (I don't know what /path/to/ is for homebrew installed executables) – steeldriver Mar 15 '20 at 19:42
  • @steeldriver OK that works! Except it keeps the timestamp for each line the same. I tried to use the -i flag but had strange output. – John Mar 15 '20 at 19:59
  • The lines will have the same timestamp if they are written at the same time - perhaps that's simply because urlwatch is buffering its output? – steeldriver Mar 15 '20 at 20:05
  • % signs must be escaped for the same reason as discussed here How can I execute date inside of a cron tab job? – steeldriver Mar 15 '20 at 20:26
  • @steeldriver Thank you. That was the issue. Please add these comments as the answer so I can vote for it. urlwatch is definitely buffering its output. Time is exactly the same for each line including milliseconds. Not sure of a way around this. – John Mar 15 '20 at 20:33
  • Aha! https://unix.stackexchange.com/q/559801/5132 again, except that it's MacOS. – JdeBP Mar 16 '20 at 10:33

1 Answers1

1

If you have (or can obtain, from homebrew for example) the ts timestamp utility, then you should be able to do something like

/usr/local/bin/urlwatch | /path/to/ts >> /Users/john/cronjobs/urlwatch.log

(/path/to/ts would typically be /usr/bin/ts on Linux systems; it may be another location on OSX). From man ts:

DESCRIPTION
       ts adds a timestamp to the beginning of each line of input.

If you want to specify a non-default time format then you can do so but remember that the % sign has special significance in cron and must be escaped. See for example

You may find that groups of lines are given the same timestamp - that may be because the program that is generating them is buffering its output. If either the unbuffer or stdbuf utilities is available for your system, it may be possible to enforce line-buffering as described here:

steeldriver
  • 81,074
  • @JdeBP While I accepted the answer using ts as the solution it would have been better if we had found a solution using OS X native tools. – John Mar 16 '20 at 16:49