This question is intended to serve as an information post for others to find, since I fortunately already discovered a solution.
I tried to run a Wordpress-CLI command as a cron-job in crontab on a Cloudways server. The command runs without any issues directly in the terminal, but fails with a Fatal PHP Error when started with crontab.
The base command for the Wordpress-CLI looks like this:
wp migratedb profile [id]
Since the context in which crontab runs is generally unknown and the $PATH variable might not be available, I modified the command to provide the necessary absolute paths:
/usr/local/bin/wp migratedb profile [id] --path=/absolute/path/to/wordpress/core/files
Again, this modified command runs also perfectly without any issues when started from the terminal.
The final crontab entry looks something like this:
0 5 * * * /usr/local/bin/wp migratedb profile [id] --path=/absolute/path/to/wordpress/core/files
When run from the scheduler, it but produces the following error:
PHP Fatal error: require(): Failed opening required 'wp-salt.php' (include_path='.:/usr/share/php') in phar:///usr/local/bin/wp/vendor/wp-cli/config-command/src/Config_Command.php(444) : eval()'d code on line 34
After some experimenting, I realized that this error happens for all WP-CLI commands, except the very basic
wp --info
, which produces the following output:
OS: Linux 4.19.0-21-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
Shell: /bin/sh
PHP binary: /usr/bin/php7.4
PHP version: 7.4.33
php.ini used: /etc/php/7.4/cli/php.ini
MySQL binary: /usr/bin/mysql
MySQL version: mysql Ver 15.1 Distrib 10.4.20-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
SQL modes:
WP-CLI root dir: phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir: phar://wp-cli.phar/vendor
WP_CLI phar path: [absolute/path/to/user/home/directory]
WP-CLI packages dir:
WP-CLI cache dir: [absolute/path/to/user/home/directory]/.wp-cli/cache
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 2.7.1
I also tried the following adaptions without any luck:
Downloading a fresh wp-cli.phar and using it for the command.
Reviewing and changing all permissions of used folders
Trying to run the cronjob as a different user
Changing the command by using
/usr/bin/php
to run thewp-cli.phar