Cron Job Log File With Timestamp

Your software
My Mautic version is: 4.2.1
My PHP version is: 7.4.28
My Database type and version is: mysqli - 10.2.43-MariaDB

Your problem
I have successfully setup cron jobs in CPANEL for all of the necessary functions.

A question I have is this:

When looking at the cronjob path:

/usr/local/bin/php /home/frugal52/public_html/EMAIL-mautic/bin/console mautic:segments:update > /dev/null 2>&1

I see the part at the end “> /dev/null 2>&1” and have learned that this discards the cronjob output.

I have successfully tried outputing that to a logfile I created by changing it the following:
/usr/local/bin/php /home/frugal52/public_html/EMAIL-mautic/bin/console mautic:segments:update >> /home/frugal52/logs-cronjobs/segments-update.txt 2>&1

This works and appends the log file as desired, however, It does not provide a timestamp for each entry. Is there an easy way to do this through cpanel?

Thanks in advance for your help and suggestions!

  • George

So I did some research and came up with an answer for my situation that I thought I would share in case it helps others.

First Off - let me describe my setup. I am hosting my Mautic installation on a VPS with root access. My hosting company is Inmotion Hosting. The setup uses WHM / CPANEL.

This method appends to the beginning of a given log file the contents of a divider-entry.txt file then pipes a date/timestamp entry to the log file and then pipes the results of running the Mautic Command to said log file. The steps I took are below:

FIRST: I created a folder under my HOME directory “/home/[USERNAME]/logs-cronjobs” where I store my log files. I also put inside that folder a text file named divider-entry.txt that contains some text such as the following (NOTE THAT LINE 1 & 3 ARE BLANK WITH A CARRIAGE RETURN OR ENTER):

LINE 1: " "
LINE 2: “X-----DIVIDER BETWEEN CRON JOB ENTRIES-----X”
LINE 3: " "

SECOND: I then setup the cron job from the cPanel as follows:
MINUTE=0,15,30,45
HOUR=*
DAY=*
MONTH=*
WEEKDAY=*

COMMAND=

cat /home/[USERNAME]/logs-cronjobs/divider-entry.txt >> /home/[USERNAME]/logs-cronjobs/segments-update.txt 2>&1 && date >> /home/[USERNAME]/logs-cronjobs/segments-update.txt 2>&1 && /usr/local/bin/php /home/[USERNAME]/public_html/EMAIL-mautic/bin/console mautic:segments:update >> /home/[USERNAME]/logs-cronjobs/segments-update.txt 2>&1

The resulting output looks something like this:

X-----DIVIDER BETWEEN CRON JOB ENTRIES-----X

Fri Apr 1 22:01:01 EDT 2022
Rebuilding contacts for segment 1
0 total contact(s) to be added in batches of 300
0 total contact(s) to be removed in batches of 300
0 contact(s) affected

X-----DIVIDER BETWEEN CRON JOB ENTRIES-----X

Fri Apr 1 22:15:02 EDT 2022
Rebuilding contacts for segment 1
0 total contact(s) to be added in batches of 300
0 total contact(s) to be removed in batches of 300
0 contact(s) affected

NOTE: You can also replace the “cat /home/[USERNAME]/logs-cronjobs/divider-entry.txt >> /home/[USERNAME]/logs-cronjobs/segments-update.txt 2>&1” with “echo “XXXXX----NEW ENTRY----XXXXX” >> /home/frugal52/logs-cronjobs/segments-update.txt 2>&1” and get a similar result except without the spaces.

I hope this helps others who want to do something similar.

Have a Great Day!
:grinning:
GEORGE

"The persistent exercise of a little extra effort is one of the most powerful forces contributing to success" - Grit