-1

Currently I have a python script that is running in a cronjob. I am redirecting any standard error from the script to append to a log file. I am trying to add a timestamp to the beginning of each write and cannot figure out how to do this in my crontabjob one-liner.

Cronjob:

*/5 * * * * /usr/bin/python .../jira_ticket.py 2>> .../error.log

Thanks in advance for any assistance.

UPDATE: SOLVED

I have solved my issue using the logging module in python. This module needs to be installed via pip.

` import logging

logging.basicConfig(filename='.../error.log', filemode='a', format='\n\n\n%(asctime)s - %(levelname)s - %(message)s') `

So now my errors in the log appear as:

2018-10-30 16:55:03,259 - ERROR - Exception Occured

Followed by the error.

k3nd0r
  • 1
  • 2
  • 4
    Possible duplicate of [How to add timestamp to STDERR redirection](https://stackoverflow.com/q/1507674/608639), [Redirect stdout and stderr, with timestamps on stderr only](https://unix.stackexchange.com/q/45921/56041), [Redirect STDERR and STDOUT to file with timestamp on each line](https://superuser.com/q/1054045/173513), etc. – jww Oct 25 '18 at 19:08

1 Answers1

0

Basically, you can prepend any output message thanks to gawk, this is a sample:

command | gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }'

You can do the same thing BUT, you'll be forced to do something with the standard output to avoid mixing everything in the same log file.

For instance:

/usr/bin/python ../jira_ticket.py 1>/tmp/myStandardOutput |& gawk '{ print strftime("[%Y-%m-%d %H:%M:%S]"), $0 }' /tmp/error.log

Notice the & after the pipe, meaning you pipe the error output, in addition to the standard output (which here has already been redirected into a dedicated log file).

Bsquare ℬℬ
  • 4,423
  • 11
  • 24
  • 44