include_once not working when using in shell script

316 Views Asked by At

I am running a php file using shell script like ./first.sh

In first.sh I am running a php file using following command

COMMAND="/usr/bin/php /home/mydirectory/public_html/members/cron/cron.php"

Under cron file I have this code

mail('[email protected]','fff','aaa');

include_once '/home/mydirectory/public_html/members/wp-config.php';

mail('[email protected]','fff1','aaa1');

when I am executing .sh file I am only getting first email and not getting second email. Below second email I have written some more coding to fetch wordpress posts but not working. And if I am hitting the cron.php in browser using domain url, it is working absolutely fine.

I have tested so many other stackoverflow answers but nothing is working for me.

Please help me.

1

There are 1 best solutions below

1
On

If you examine wp-config.php (I suppose this is a WordPress configuration file), then at the ~bottom of the script you will see the line

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

Unfortunately, WP includes a "bootstrap" file directly into config... wp-settings.php includes many other wp files and initializes things etc. So, this must be debugged at Wordpress level. WP ends execution somewhere in wp-settings.php or other include file, so your php cron exits/dies. Currently, I have not a local wp installation to debug (sorry), but I am 99% sure wp ends execution.

You can hardcode the info you need from wp-config like database host, username etc. into your cron.

Otherwise, you may parse the php file with token_get_all to retrieve the values (I had faced similar problem 2 years ago, unfortunately I have the code in old job office). http://php.net/manual/en/function.token-get-all.php