Run wp cli commands with cron job
Asked Answered
N

2

10

Is it possbile to run WP cli commands with a cron job? Because I'm setting up a demo site, and want the site to be reset every 12 hour. And i thought that with wp cli, i can reset the installation. Import a file with the right content and so on.

Or do you guys have any better idea how to do this?

Nuncia answered 4/8, 2016 at 11:24 Comment(2)
Can you configure wp_schedule_event() codex.wordpress.org/Function_Reference/wp_schedule_event for youк needs?Decibel
I also remember I read about wp-cli.org/commands/cronDecibel
G
10

Yes, you can.

Here's a line from my crontab file. (You can edit it by running crontab -e)

*/10 * * * * /var/www/vendor/wp-cli/wp-cli/bin/wp --path=/var/www/ update-credits

This runs my WP-CLI command update-credits every 10 minutes.

NOTE: I'm using WP-CLI as a Composer dependency in that website. The path to your wp binary may vary.

Goddess answered 23/8, 2017 at 15:44 Comment(1)
Using full path to wp binary file is the key. It works in my case. Using only wp will fail.Plop
O
2

Absolutely.

Assuming you already have WP-CLI installed in your server, you could use something like the below line in your /etc/crontab file:

0 */12 * * * www-data wp --path=/var/www/yoursite.com db reset
Owens answered 4/9, 2017 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.