• If you are still using CentOS 7.9, it's time to convert to Alma 8 with the free centos2alma tool by Plesk or Plesk Migrator. Please let us know your experiences or concerns in this thread:
    CentOS2Alma discussion

Resolved Script runs in browser but not asscheduled job

GrMeezer

New Pleskian
Hi all,

I am trying to move my site from a VPS with cpanel to my own server with Plesk on linux. Everything is running fine apart from some php scripts which I have to email contact forms, tidy databases etc.

I have a domain my-domain.com and a subdomain tools.my-domain.com

If I run tools/my-domain.com/email-enquiries.php then it executes no problem.

If I setup a scheduled task (under the subdomain tools.my-domain.com and tell it to run the php file /tools.my-domain.com/email-enquiries.php then click "run now" it says completed with error and which I got to look at the details it says:

Code:
Task "tools.my-domain.com/email-enquiries.php" completed with error in 0 seconds, output:

PHP Warning: include(./header.php): failed to open stream: No such file or directory in /var/www/vhosts/my-domain.com/tools.my-domain.com/email-enquiries.php on line 9
PHP Warning: include(./header.php): failed to open stream: No such file or directory in /var/www/vhosts/my-domain.com/tools.my-domain.com/email-enquiries.php on line 9
PHP Warning: include(): Failed opening './header.php' for inclusion (include_path='.:/opt/plesk/php/7.4/share/pea...


A previous post with a similar topic suggested something to do with permissions - but the file email-enquiries.php and the file it is trying to INCLUDE are both owned by the same user (who is the only user I have)

Can anyone suggest where I am going wrong? There's clearly nothing wrong with the php as it executes in the browser - and executes both in the browser and via cron on my current (albeit not plesk) server so it must be something to do with my configuration but I'm stumped...


Thanks :)
 
By default a cronjob has no sense of the "location" of the script it's running. I know that sounds weird, but it's not aware of the directory hierarchy. Hence included files cannot be localized. To solve this use the full path for files you which to include in your script. You can do so by prepending your include with the magic constant __dir__.

So in stead of using
PHP:
include(./header.php);
Use
PHP:
include(__DIR__ . '/header.php');
 
Last edited:
Thank you for your reply - makes sense.

Since this works fine on my current (cpanel) server but not plesk is it possible that this can be set in the php configuration? This is not the only script I need to run and I am wondering if there is an easier fix than amending the code in all my php files?
 
Ah that looks like just the thing - thank you so much sorry wasn't meaning for you to do my googling for me!

There is clearly some kind of solution that doesn't invole rewriting my php - will figure it out and update this thread.
 
For anyone coming across this in future - I did not test the suggestion made by Rasp above (changing php files to have include(__DIR__ . '/header.php'); ) - although I don't have a huge number of scripts, there are just enough with sufficient complexity that editing my source code would be a non-trivial job.

Following the link in his second post and adjusting the suggestion for the fact that one needs to include the full path to both the script AND the PHP binarry file, I was able to make this work by chosing a command in the scheduler instead of a php script and using the following as my command to run:

Code:
cd /var/www/vhosts/my-domain.com/tools.my-domain.com/&& /opt/plesk/php/8.0/bin/php -f email-enquiries.php

This command sucessfully runs the script -either by manually pressing the 'run now' button or by lettingthe scheduler do it's think - but without requiring any alteration to the source php script.

@Rasp thank you very much for your help - I am most grateful :)
 
Back
Top