My previous post about executing the WP All Import cron jobs with PHP cli didn’t work too well. Apparently the processing part still stops executing after a while. I ended up with import jobs stating “last activity 5 hours ago”.
Still I don’t want to run this through the webserver. So I wrote a little wrapper shell script to execute my imports in cron. It checks if the line “is not triggered” was partially returned. The wp cron script would return “<p>Import #26 is not triggered. Request skipped.</p>” if you execute the processing action without triggering it first. In other words: the import is really complete now.
I put the shell script outside my document root, and it uses the “logs” directory to store the log files. So make sure this directory is writable and points to the right path.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#!/bin/bash while getopts “:j:” opt; do case $opt in j) jobId=$OPTARG ;; \?) echo “Invalid option: -$OPTARG” >&2 ;; :) echo “Option -$OPTARG requires an argument.” >&2 exit 1 ;; esac done if [ “$jobId” = “” ]; then echo No job id exit 1 fi # Set magic variables for current FILE & DIR __FILE__=“$(test -L “$0” && readlink “$0” || echo “$0“)” __DIR__=“$(cd “$(dirname “${__FILE__}”)“; echo $(pwd);)” LOGFILE=“${__DIR__}/logs/wpai_cron_${jobId}.log” CURLOG=“${__DIR__}/logs/wpai_cron_${jobId}_current.log” DONE=0 function log { echo “$(date): $*” >>$LOGFILE } log “Start import for jobID $jobId” cd $__DIR__/public_html php –e –r ‘parse_str(“import_key=xxYourKeyxx&import_id=’$jobId‘&action=trigger”, $_GET); include “wp-cron.php”;’ >>$LOGFILE 2>&1 sleep 1 while [ $DONE –eq 0 ] do php –e –r ‘parse_str(“import_key=xxYourKeyxx&import_id=’$jobId‘&action=processing”, $_GET); include “wp-cron.php”;’ >$CURLOG 2>&1 cat $CURLOG >>$LOGFILE DONE=$(grep ‘is not triggered’ $CURLOG | wc –l) sleep 1 done rm $CURLOG log “End of import for jobId $jobId” log “” log “” |
Now just put this in your /etc/cron.d/yoursite:
1 2 3 4 5 6 |
# # Run product imports every morning at 6 am # 0 6 * * * yourwebsite /home/yourwebsite/wpai_cron.sh –j 26 5 6 * * * yourwebsite /home/yourwebsite/wpai_cron.sh –j 27 10 6 * * * yourwebsite /home/yourwebsite/wpai_cron.sh –j 28 |
Leave a Reply