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/)
  • Does your system provide the ts command? – steeldriver Mar 15 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 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 at 19:20
  • @steeldriver ts installed. What do you suggest? – John Mar 15 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 at 19:42
0

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:

| improve this answer | |
  • @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 at 16:49

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.