Fix fatal error for inlcude path and included file not existed using include or require for cron job

When running php in cron, the include and require path may not work.

Eg. when your file has below require or include statement:

require '../includes/common.php';

You will get a fatal error when running using php-cli or php-cli in cron

# php-cli
php /home/username123/public_html/cron/mycronjob.php
# cron
* * * * * php /home/username123/public_html/cron/mycronjob.php

Error got:

Fatal error: require(): Failed opening required '../includes/common.php'
 (include_path='.:/usr/lib/php:/usr/local/lib/php') in
 /home/username123/public_html/cron/mycronjob.php on line 2

To fix this, you need to use php to set include_path attributes so when running cron it'll set the path so the script will learn where to look for the include / require files.

set_include_path

reference: http://php.net/manual/en/function.set-include-path.php

example:

set_include_path('/home/username123/public_html/includes/');
require 'common.php';

My experiences of changing php cron job running by wget to php-cli /usr/bin/php

tool used to convert the cron interval to human readable time: https://crontab.guru/

This is one of my real world experiences.

Lately we migrated some websites with the entire WHM and all the cron jobs running as is.

There were about 12 cron jobs running, something like below:

59 23 6 /usr/bin/wget -O /dev/null "http://www.somedomain.net.au/some-api-data.php" >/dev/null 2>&1

However because of the domain and IP routing changes, we could no longer run wget for any of the php files under these domains.

So my alternative is to move these script running via wget to internally running via php-cli.

Below is the approach:

Firstly created a test cron.

SSH as root, put a small php script to output the time stamp and run as cronjob every minute as below. ( crontab -l will show this line at the bottom)

          • /usr/bin/php /home/website/public_html/date_cron_test.php >> /usr/logs/date_cron_test.log 2> /dev/null
            Successfully generates the log file and had correct record for outputs of each round.

            Then change all the cron jobs like below

            59 23 6 /usr/bin/wget -O /dev/null "http://www.somedomain.net.au/some-api-data.php" >/dev/null 2>&1

to

59 23 6 /usr/bin/php /home/somedomain/public_html/some-api-data.php >> /usr/logs/some-api-data.log 2> /dev/null

Change all the old wget ones to the /usr/bin/php php-cli ones accordingly.

Also the php needs to be set mode with x as executable, eg 755, this depends on your actual server settings.

Finally run service crond reload

It's all set up. All we need to do is checking the cron and php log later to verify.

 

References:

  • Cron job executing php trouble shooting: http://stackoverflow.com/questions/7397469/why-is-crontab-not-executing-my-php-script
  • crontab php file and output to log file result (append mode): http://stackoverflow.com/questions/9456424/crontab-php-file-and-output-to-log-file-result
  • Meanings of 2>&-, 2>/dev/null, |&, &>/dev/null and >/dev/null 2>&1: http://unix.stackexchange.com/questions/70963/difference-between-2-2-dev-null-dev-null-and-dev-null-21
  • cron "BAD FILE MODE": https://www.redhat.com/archives/rhl-list/2005-February/msg02458.html

Manually edit vhost file on WHM-Cpanel server

Manually edit vhost file on WHM-Cpanel server

If you need to manually edit the vhost file ona server that is running WHM and cPanel then you need to edit the include files which are used to build the vhost file. The include files for each account are held in the following location:

/var/cpanel/userdata/*accountname*

The main file would be

/var/cpanel/userdata/*accountname*/yourdomain.com

Parked domains and subdomains are held in the "main" file in the same folder. For an account, "yourdomain" and the URL yourdomain,com you would edit:

/var/cpanel/userdata/yourdomain/yourdomain.com

After editing the file you need to run the following three commands to rebuild the vhost file.

/usr/local/cpanel/bin/apache_conf_distiller --update
 /scripts/rebuildhttpdconf
 /etc/init.d/httpd restart