WP All Import cron from CLI

The WP All Import plugin can be used to import data from xml feeds. We use it in combination with WooCommerce. To automatically update your products and/or stock information, you should run this from a cron job. Unfortunately WP All Import didn’t do a very nice job on integrating this with the WordPress cron system, because I feel that the scheduling should be manager from your WordPress install instead of cronjob settings on the server.

WP All Import suggests that you set a cron job where you request an URL with wget. They already noticed a possible issue with this approach, which is timeouts. Therefore they created two actions: one to trigger the import, and a second one to process the import. When a timeout occurs, the process action should continue where it left of.

In our situation (nginx + php-fpm) this didn’t seem to work very well. The script was aborted after 30 seconds (which is what we have configured on the server ourselves), but when I re-requested the processing action, it said I should run the trigger action first. So, did the processing complete or not?

Since I don’t like running web requests from cron anyways, I searched for a method to run the import with PHP cli. This way we don’t have timeouts and the script will execute nicely. Also you can skip the “The processing URL should be run every two minutes because it may not finish your import in one run.” because it’s very unlikely that the script will timeout.

Enough chichat, here’s the line I added to /etc/cron.d/mywebshop:

Don’t forget to change the path to your WordPress install, and the import_key.

Comments

8 responses to “WP All Import cron from CLI”

  1. Dave Avatar
    Dave

    Thanks for the article. A dead easy webcron https://www.easycron.com/ also works.

    1. rolandow Avatar
      rolandow

      Dave, although this seems like an advertisement to me, I guess easycron could be a nice tool. But the cron jobs are still executed through the webserver, and that’s exactly the problem with WP All Import. Easy cron will not solve this. Executing the job with php cli will.

  2. John Avatar
    John

    rolandow thanks so much for this, was looking long and hard for a solution, just a few questions. If we have multiple imports would we just add the other ones below the first line, and second what if we want an email to be sent when completed ? also does this method keep logs anywhere ? because with wpallimports method the users home directory filled up with hundreds of logs
    thanks in advance
    John

    1. John Avatar
      John

      one more thing if we need to run the importer more than once a day, do we put it in the cron.hourly folder ?

    2. rolandow Avatar
      rolandow

      Please check my update on this matter: https://devlog.rolandow.com/2014/11/wp-import-cron-cli-update

      Eventually I created a shell script that can be executed from shell. You can run is with an argument to define what WP All Import job you want to run.

      If you want a mail when it’s done, you could add this mail command to the shell script. At line 49 add:
      echo “Job $jobId is finished” | mail yourmail@gmail.com -s “WP All Import cron job $jobId finished”

      Good luck!

  3. […] 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”. […]

  4. Rinaldo Avatar

    hey, big thanks!
    I didn’t use the cron anyway, but I run it directly on terminal and it works pretty good.

    once again, thanks!

  5. MK Avatar

    Hi,
    Thanks for this article and solution. You’ve made my day!

Leave a Reply

Your email address will not be published. Required fields are marked *