I have a bash script with various WP-CLI commands to run a backup, check & optimize db tables, run a security vulnerability check, check available core, plugin, and theme updates, and then email the report to me.
If I run this script from the command line, it works perfectly. However, if I schedule a cronjob, the script runs, but the output for all WP commands are as such:
/usr/local/bin/dev-maintenance-check.bash: line 70: wp: command not found
I've tried various different cron job formats to no avail. This is the current cronjob:
0 1 * * 0,3 bash /usr/local/bin/dev-maintenance-check.bash
My WP-cli installation is at usr/local/bin/wp. Again, if I run "bash /usr/local/bin/dev-maintenance-check.bash" in the command line, that works.
To me, it sounds like the wp-cli needs to be called in the script. I feel confident that I've exhausted my search on Google on the wp-cli docs. Any help would be appreciated.
As your script is a shell script (/bin/sh), then your PATH entries in .bashrc will not be read as that is for the bash (/bin/bash) interactive shell.
To make your PATH entries available to /bin/sh scripts run by a specific user, add the PATH entry to the .profile file in that users home directory.
See https://unix.stackexchange.com/a/163126/72176
So, the interesting part is not your crontab, but your script. Simply call the absolute or relative location to wp
or fix it as suggested above and it should work.
Absolute:
#!/usr/bin/env bash
cd /var/www/wordpress
/usr/local/bin/wp cache flush
Relative (WP-CLI installed as local Composer dependency):
#!/usr/bin/env bash
cd /var/www/wordpress
vendor/bin/wp cache flush