PHP Fatal Error: Unable to open required 'wp-salt.php' when running WP-CLI command as cron-job in crontab
P粉253518620
P粉253518620 2024-03-28 09:25:13
0
1
494

This question is intended to be an informational post for others to find because luckily I've already found the solution.

I tried running the Wordpress-CLI command as a cron job in crontab on the Cloudways server. The command runs without any issues directly in the terminal, but fails with a fatal PHP error when launched using crontab.

The basic commands of Wordpress-CLI are as follows:

wp migeratedb configuration file [id]

Since the context in which crontab is run is usually unknown, and the $PATH variable may not be available, I modified the command to provide the necessary absolute path:

/usr/local/bin/wp migeratedb configuration file [id] --path=/absolute/path/to/wordpress/core/files

Similarly, this modified command also runs perfectly without any issues when launched from the terminal.

The final crontab entry will look like this:

0 5 * * * /usr/local/bin/wp migeratedb configuration file [id] --path=/absolute/path/to/wordpress/core/files

When run from the scheduler it 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 experimentation, I realized that this error occurs for all WP-CLI commands except the very basic

wp --info

, producing 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 adaptations without success:

  • Download the new wp-cli.phar and use it with the command.

  • View and change all permissions of used folders

  • Try running the cronjob as another user

  • Use /usr/bin/php Change the command to run wp-cli.phar

P粉253518620
P粉253518620

reply all(1)
P粉790187507

I finally figured out that this error was related to the way the hosting provider (in this case Cloudways) set up the WordPress configuration in the wp-config.php file.

The salt and secret authorization keys are stored in a separate file, wp-salt.php referenced in the error message. It can be referenced directly in the configuration file via: require('wp-salt.php'). Since this is not part of WordPress core and crontab is running in a different environment, it cannot determine the correct directory where the file is located and require() fails with a fatal error.

To fix this, change the line in wp-config.php to require(__DIR__.'/wp-salt.php'); so that the file always Referenced from the same directory as the configuration file.

Another option is to remove the line entirely and replace it with the contents of the wp-salt.php file, just like Wordpress core does.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template