[{"data":1,"prerenderedAt":173},["ShallowReactive",2],{"\u002Fblog\u002F2015-10-08-better-wordpress-cron-jobs-crontab-wpcli":3},{"title":4,"path":5,"description":6,"excerpt":7,"body":8,"date":172},"Better Wordpress Cron Jobs with crontab and WP CLI","\u002Fblog\u002F2015-10-08-better-wordpress-cron-jobs-crontab-wpcli","WP Cron jobs can be powerful tools, allowing you to schedule tasks that need to happen automatically. Unfortunately, by default, this only runs on a page visit.",null,{"type":9,"value":10,"toc":164},"minimark",[11,14,17,27,32,40,44,47,50,55,58,63,66,71,74,79,85,90,98,104,109,112,115,120,131,135,142,147,153,156,161],[12,13,6],"p",{},[12,15,16],{},"That means two things for you:",[18,19,20,24],"ul",{},[21,22,23],"li",{},"Jobs only run when there's visitors actively on the site when its supposed to run",[21,25,26],{},"A log running job will hold up the page render for the unlucky visitors that trigger it",[28,29,31],"h3",{"id":30},"so-how-do-you-do-that-better","So, how do you do that better?",[12,33,34,35,39],{},"Crontab is a great solution for running cron jobs, its what your server does for its own jobs. But getting it to execute tasks can be a little tricky. Theres workaround requiring your server to call ",[36,37,38],"code",{},"curl http:\u002F\u002Fyoursite.com\u002Fwp-cron.php",", but that relies on curl and that domain is resolvable from the server.",[28,41,43],{"id":42},"wp-cli-to-the-rescue","WP-CLI to the rescue",[12,45,46],{},"WP CLI has the ability to run cron jobs itself. It does this intelligently, obeying the set schedule even when its run outside the task's window. So that means we can safely run that command on a minute interval.",[12,48,49],{},"This command runs a cron job, but you notice, it needs the hook name and can only run one at a time",[12,51,52],{},[36,53,54],{},"wp cron event run cron_job_hook_name",[12,56,57],{},"This command lists all cron jobs available",[12,59,60],{},[36,61,62],{},"wp cron event list",[12,64,65],{},"So, we take that and only get the hook, no formatting.",[12,67,68],{},[36,69,70],{},"wp cron event list --fields=hook --format=ids",[12,72,73],{},"Then we're going to wrap that in a bash loop so that each hook that gets returned runs the cron job.",[12,75,76],{},[36,77,78],{},"for i in $(wp cron event list --fields=hook --format=ids); do wp cron event run $i; done",[12,80,81,82],{},"That'll run them all! But since this is a cron job, it'll run outside our normal bash environment. So, we need to specify where the site is with: ",[36,83,84],{},"--path=\u002Ffull\u002Fpath\u002Fto\u002Fsite",[12,86,87],{},[36,88,89],{},"for i in $(wp cron event list --fields=hook --format=ids --path=\u002Ffull\u002Fpath\u002Fto\u002Fsite); do wp cron event run $i --path=\u002Ffull\u002Fpath\u002Fto\u002Fsite; done",[12,91,92,93,97],{},"Notice how we have to do that in the list command ",[94,95,96],"em",{},"and"," the run command.",[12,99,100,101],{},"Sometimes, depending on how you set up WP-CLI, you'll need to specify it's location too, like ",[36,102,103],{},"\u002Fusr\u002Flocal\u002Fbin\u002F",[12,105,106],{},[36,107,108],{},"for i in $(\u002Fusr\u002Flocal\u002Fbin\u002Fwp cron event list --fields=hook --format=ids --path=\u002Ffull\u002Fpath\u002Fto\u002Fsite); do \u002Fusr\u002Flocal\u002Fbin\u002Fwp cron event run $i --path=\u002Ffull\u002Fpath\u002Fto\u002Fsite; done",[12,110,111],{},"Awesome, so this should run great, lets put it in a cron job.",[12,113,114],{},"That runs all WP cron jobs, every 5 minutes (*\u002F12 is 60 minutes\u002F12 = 5 minutes)",[12,116,117],{},[36,118,119],{},"*\u002F12 * * * * for i in $(\u002Fusr\u002Flocal\u002Fbin\u002Fwp cron event list --fields=hook --format=ids --path=\u002Ffull\u002Fpath\u002Fto\u002Fsite); do \u002Fusr\u002Flocal\u002Fbin\u002Fwp cron event run $i --quiet --path=\u002Ffull\u002Fpath\u002Fto\u002Fsite; done",[12,121,122,123,126,127,130],{},"Note I added ",[36,124,125],{},"--quiet"," to the ",[36,128,129],{},"run"," command so that I don't get an email every time this runs.",[28,132,134],{"id":133},"root-problems","root problems",[12,136,137,138,141],{},"Sometimes you'll have to run this as a root user, which WP CLI will warn you about. If you're comfortable with running this under root, WP-CLI has ",[36,139,140],{},"--allow-root"," as well",[12,143,144],{},[36,145,146],{},"*\u002F12 * * * * for i in $(\u002Fusr\u002Flocal\u002Fbin\u002Fwp cron event list --fields=hook --format=ids --path=\u002Ffull\u002Fpath\u002Fto\u002Fsite --allow-root); do \u002Fusr\u002Flocal\u002Fbin\u002Fwp cron event run $i --quiet --path=\u002Ffull\u002Fpath\u002Fto\u002Fsite --allow-root; done",[12,148,149,150,152],{},"Again, note that you must ",[36,151,140],{}," on both commands.",[12,154,155],{},"There ya go, now you can disable WP's cron runner by adding this to your wp-config.php",[12,157,158],{},[36,159,160],{},"define( 'DISABLE_WP_CRON', true );",[12,162,163],{},"Hope that helps!",{"title":165,"searchDepth":166,"depth":166,"links":167},"",2,[168,170,171],{"id":30,"depth":169,"text":31},3,{"id":42,"depth":169,"text":43},{"id":133,"depth":169,"text":134},"2015-10-08T00:00:00.000Z",1786108218184]