CiviCRM Cron Job Script with Wordpress

Posted: 2020-08-13 | Updated: 2020-08-16 | Read Time: 8 minute(s)

To make CiviCRM work in Wordpress, you need to have cronjobs scheduled on your system to run to certain intervals. Otherwise you have to manually run these task which will increase your manual workload. In this video, I show you how to set up this automated process.

Video

Video Transcript

AS PART OF THE CONVERSION OF MY WEBSITE OVER TO WORDPRESS I ALSO INSTALLED CIVI CRM AND SO SYMPATHY RM REQUIRES A CRON TASK OR A ROUTINE SCHEDULER THAT KICKS OFF VARIOUS MAINTENANCE PROCESSES WHETHER HOURLY OR DAILY AND SO WHAT I DID WAS I WROTE A SHELL SCRIPT AS WELL AS DOWNLOADED THE CIVI CRM COMMAND-LINE INTERFACE TOOL THAT WOULD ALLOW ME TO DO ALL OF THAT SO I'M GOING TO COVER THAT AND A LITTLE BIT MORE IN THIS VIDEO WHAT'S GOING ON FELLOW ENGINEERS THANK YOU FOR CHECKING OUT THIS VIDEO ONCE AGAIN SO HERE WE HAVE THE SHELL SCRIPT THAT I CREATED FOR RUNNING CIVI CRM CRIME AND SO THE REASON THAT YOU HAVE TO DO THAT IN SUPER CRM YOU HAVE A SERIES OF MAINTENANCE PROCESSES THERE PREFER TO AS SCHEDULED TASKS AND THE SYSTEM EXCUSE ME SCHEDULED JOBS AND SO I'M GOING TO ADMINISTER SYSTEM SETTINGS AND SCHEDULED JOBS WILL BRING YOU TO THIS PAGE AND SO THERE ARE VARIOUS TASKS SUCH AS CLEANING IT UP TEMPORARY DATA CHECKING FOR UPDATES CHECKING EMAILS WHETHER IT'S PROCESSING EMAILS OR FETCHING EMAILS THAT HAVE BOUNCED FROM YOUR SUBSCRIPTIONS AND SO THEY RUN AT VARIOUS INTERVALS AND IN ORDER TO ACCOMPLISH THAT CIVI CRM REQUIRES THAT YOU SET UP A TASK SCHEDULER OR A CRON JOB AND SO WHAT I DID WAS I FOUND OUT THAT THERE WAS A WAY THAT THAT COULD BE DONE USING THE CIVI CRM COMMAND-LINE TOOL AND SO IF YOU GO THROUGH THE CIVI CRM WEBSITE YOU CAN DOWNLOAD THIS CV DOT FAR FILE OR CAR I'M NOT SURE HOW IT'S PRONOUNCED I'M ASSUMING BECAUSE THEY HAVE THE PH IS FAR FAR FAR AWAY AND SO WHAT IT DOES IS IT ALLOWS YOU TO RUN VARIOUS THINGS IN CIVI CRM AND PERFORM VARIOUS ACTIONS IT IS SIMILAR TO THE THE WP COMMAND LINE OR WORDPRESS COMMAND LINE INTERFACE WHERE YOU HAVE TO RUN IT AND IT GIVES YOU A NUMBER OF THINGS AND SO IF YOU RUN IT LIKE I JUST DID HERE YOU ACTUALLY HAVE TO RUN PHP AND THEN PASS THE FILE NAME AS A ARGUMENT TO THE PHP CALL BECAUSE IT IS ACTUALLY A PHP SCRIPT OF SORTS AND SO YOU HAVE A NUMBER OF OPTIONS HERE YOU CAN MAKE CALL TO THE APIS YOU CAN CALL THE COMMAND LINE INTERFACE YOU CAN DO VARIOUS THINGS HERE I'M NOT GONNA GO THROUGH EVERYTHING AND SO ONE THING THAT YOU CAN DO IS TO RUN THE CRIME PROCESS AND SO I HAVE THAT POOL OF DUCK AND SO HERE IS THE SHELL SCRIPT THAT I HAVE IF YOU WANT TO USE THIS IN YOUR CIVI CRM INSTALLATION YOU CAN GO TO THE REPOSITORY FOR MY WEBSITE I HAVE A LINK TO THE POSIT OR E DOWN IN THE DESCRIPTION BELOW AND YOU CAN MAKE A COPY OF THIS SCRIPT AND SO BASICALLY WHAT IT DOES IS IT CHECKS IT STARTS OFF WITH THE CURRENT DATE AND THIS URL PROVIDES MORE INFORMATION ABOUT WHERE I GOT THE CONCEPT OF HOW TO BUILD THIS SCRIPT OUT BUT IT STARTS WITH THE CURRENT DATE AND THE REASON I PUT THE CURRENT DATE IN HERE IS SO YOU CAN SEE WHEN IT STARTED AND WHEN THEY END IT I'LL PROBABLY MAKE SOME MORE ADJUSTMENT TO THIS BEFORE ACTUALLY ROLLING IT OUT TO MY PRODUCTION ENVIRONMENT BUT IT'S CHECKING TO MAKE SURE THAT ARGUMENT ONE IS NOT EMPTY AND THE REASON BEING IS BECAUSE ARGUMENT 1 IS GOING TO BE THE CURRENT DIRECTORY OF WHERE THIS WHERE YOUR WORDPRESS INSTALL IS AND THE REASON BEING IS BECAUSE IT NEEDS TO KNOW WHERE WORDPRESS IS ON THE FILE SYSTEM IN ORDER FOR IT TO GO FROM THERE DOWN INTO THE FILE SYSTEM TO THE CIVIC CRM STUFF TO BE ABLE TO RUN THE NECESSARY COMMANDS THAT IT HAS TO RUN AND SO WHAT THIS COMMAND DOES IT JUST PRINTS OUT THE PATH THAT WAS PASSED IN AS ARGUMENT 1 TO THE SCRIPT AND THEN YOU CALL THEM PHP AND THEN THE CB DOT FARM FILE AND THEN THE NEXT PART IS YOU'RE MAKING AN API CALL TO DO JOB EXECUTE AND YOU WANT TO RUN THAT JOB EXECUTION AS USER CRON CIVI THIS USER WILL DEPEND ON HOW YOU HAVE YOUR SETUP BUT YOU NEED TO CREATE A USER ACCOUNT THAT WILL BE USED TO RUN THE CRON JOBS AND THEN YOU NEED TO SET THE CURRENT WORKING DIRECTORY TO THE LOCATION THAT YOUR WORDPRESS INSTALL IS LOCATED ON THE FILESYSTEM AND SO IF YOU DON'T PASS IN THIS DOLLAR ONE IT THROWS AN ERROR BASICALLY SAYING YOU NEED TO RUN IT AND I ACTUALLY NEED TO UPDATE THE SCRIPT NAME BECAUSE I HAVE CHANGED IT BUT BASICALLY THE SCRIPT NAME AND THEN THE FOLDER ROOT WHERE FOLDER ROOT IS THE TOP LEVEL OF YOUR WORDPRESS PROJECT AND THEN THAT IS IT AND SO WHAT YOU DO RUN THIS AND I'LL GO AHEAD AND RUN IT FOR YOU NOW ALL RIGHT SO GOING TO DO DOT OR I CAN DO BASH EITHER WAY WORKS BECAUSE IT IS A BASH SCRIPT AND SO IS CIVI CRON EXCUSE ME CBC REM UNDERSCORE CRON DOT S H AND THEN I AM GOING TO DO THE PATH TO MY WORDPRESS DIRECTORY WHICH IT IS AND MY HOME AND SO I HIT ENTER AND YOU GET THIS OUTPUT AFTERWARDS AND SO AGAIN THIS IS THE START DATE AND TIME IT'S THE END DATE AND TIME THIS IS THE PATH LOCATION THAT I PASSED IN AND THIS SECTION RIGHT HERE IS THE JSON API RESPONSE THAT CIVI CRM HAS RETURNED BACK AND IF YOU GET A IS ERROR IS EQUAL TO ANYTHING OTHER THAN ZERO THAT MEANS SOMETHING DID NOT GO RIGHT AND YOU'LL NEED TO CHECK UP BE LOGS USUALLY IF SOMETHING DOES GO WRONG THERE MAY BE A LITTLE BIT MORE OUTPUT THAN THESE LINES THAT IT DID RETURN THE LOG ERROR WILL SHOW UP IN CBC RM SO YOU COULD JUST LOG INTO SAVVY CRM AND LOOK IT'S THERE TO SEE WHAT THE PROBLEM MAY BE SO THAT PRETTY MUCH COVERS THAT PART OF THINGS AND THEN THE LAST THING IS THAT YOU WILL NEED TO RUN THIS SHELL SCRIPT BEING A CRONTAB OR SCHEDULER TASK SO NOW I WILL COVER THAT PARTICULAR PART SO ON MY SYSTEM I HAVE CRONTAB SO I'M GONNA GO AHEAD AND OPEN THAT UP AND AS YOU SEE I HAVE THE SECOND ITEM HERE WHICH RUNS THAT CIVICRM CRON SCRIPT AND I HAVE IT RUNNING EVERY 15 MINUTES HOWEVER IS NOT NECESSARY TO HAVE IT RUN IN 15 MINUTES AT A MINIMUM I WOULD SUGGEST YOU RUN IT EVERY HOUR BECAUSE THERE ARE SOME HOURLY TASKS IN THERE THAT HAVE TO BE PERFORMED AND THAT ALSO DEPENDS ON THE AMOUNT OF VOLUME OF TRAFFIC ON YOUR WEBSITE IF YOU HAVE A WHOLE LOT OF VOLUME HOUR IS DEFINITELY RECOMMENDED IF YOU DON'T HAVE AS MUCH VOLUME YOU COULD PROBABLY GET AWAY WITH 2 OR 3 OR EVEN 12 HOURS FOR THE FREQUENCY AND SO BASICALLY IT IS THE SAME COMMAND THAT I RAN SO I AM USING THE FULL PATH TO THE EXECUTABLES AND THE REASON BEING IS BECAUSE SOMETIMES WHEN YOU START A CRON JOB DEPENDING ON THE SYSTEM IT STARTS IN THE ROOT OF THE FILE SYSTEM IT MAY START IN THE HOME OF THE USER THAT IS RUNNING THAT CRON JOB IT ALL DEPENDS SO TO BE VERY EXPLICIT USE THE FULL PATH BECAUSE DEPEND ON WHERE IT STARTS IT MAY OR MAY NOT LOAD THAT RC FILE AND THE PATH VARIABLE WHICH WILL HAVE THE REFERENCE TO /BIN ALREADY POPULATED SO THAT'S WHY IN THE COMMAND LINE YOU CAN TYPE BASH AND IT KNOWS BIN BASH BUT IN CRON IT DOESN'T NECESSARILY LOAD THAT SO YOU IT'S BEST TO TYPE THE FULL THING SO IT'S NOT GUESSING AND SO GIVING THE FULL PATH TO THE SHOW SCRIPT AND THEN I'M GIVING THE FULL PATH TO THAT DIRECTORY AND THEN I AM OUTPUTTING ALL OF THE RESULTS FROM THE COMMAND THAT'S BEING RAN INTO A LOG FILE THAT IS IN MY HOME DIRECTORY AND THEN I'M COMBINING ANY ERRORS INTO THE STANDARD OUT AND ALL THAT GOES INTO THAT SAME LOG FILE THAT WAY I DON'T HAVE TWO FILES THAT I HAVE TO CHECK IT'S EVERYTHING IS IN ONE FILE AND IT'S IN THE ORDER THAT IT OCCURRED AND SO I WILL ALSO SHOW YOU THIS LONG VOWEL SO GO HERE AND DO CATS BECAUSE THE CONTENTS ARE NOT THAT LONG AND SO HERE YOU SEE WE HAVE THE SAME THING FROM WHEN I RAN IT ON THE COMMAND LINE DATE THE PATH AND THE JSON RESPONSE AND THEN THE END DATE AND TIME SO THAT WILL WRAP IT UP FOR THIS PARTICULAR VIDEO IF YOU NEED HELP SETTING UP YOUR CRON JOB ON CBC RM FEEL FREE TO REACH OUT TO ME ON INSTAGRAM AND TWITTER YOU CAN ALSO REACH OUT DOWN BELOW BY LEAVING A COMMENT WITH WHATEVER ISSUE THAT YOU'RE HAVING AND I'LL BE SURE TO RESPOND BACK TO YOU WITH ANY ADVICE OR SUGGESTIONS THAT I CAN OFFER ALSO FEEL FREE THAT IF THIS VIDEO HAS HELPED YOU GIVE IT A THUMBS UP AND YOU CAN CHECK OUT MY WEBSITE BE ALMOST ENGINEER CALM WHERE I'LL HAVE MORE INFORMATION RELATED TO THIS AND CIVICRM IN THERE AVAILABLE FOR YOU TO READ UP ON SO UNTIL NEXT TIME PEACE OUT [MUSIC]

Published: 2020-08-13
Updated: 2020-08-16
Author: Kenny Robinson, @almostengr
Category: Technology
Keywords: wordpress, civicrm, cron, automation, wordpress command line, wp-cli
Video URL: https://www.youtube.com/watch?v=pcLIq3rex0Y
Read Time: 8 minute(s)