Search code examples
phpbatch-filescheduled-taskswindows-server-2012-r2windows-task-scheduler

Run PHP script with Windows Task Scheduler, including files fails


I'm given a Windows 2012R2 machine with XAMPP. I need to schedule a task that runs every minute. Every minute a PHP script should be executed. This is configured correctly, I'm using a .BAT file as action in Task Scheduler.

The problem is that when I test the .BAT file, it works like a charm (execute the .BAT file from the folder it's in). But when Task Scheduler executes the .BAT file it fails. The script is then unable to include some files that the PHP script calls. I'm guessing that's because Task Scheduler doesn't execute the script from the directory the PHP script is in. CronjobBase.php has a line that includes MyIncludes.php (examplary, in real life a bootstrap file is included)

This is my .BAT content (there's no difference in using or leaving the quotes with the CD command):

cd "D:\xampp\htdocs\MyApp\App\Cronjobs\"
"D:\xampp\php\php.exe" -f CronjobBase.php MyCronJob

I'm trying to CD into the Cronjobs folder to execute CronjobBase.php with argv[1] as MyCronJob (which should include MyCronJob.php from CronjobBase.php). It tells me CronjobBase.php is not found. I'm guessing CD-ing into the Cronjobs folder fails.

When I try "D:\xampp\php\php.exe" -f D:\xampp\htdocs\MyApp\App\Cronjobs\CronjobBase.php MyCronJob as .BAT content the inclusion of 'MyIncludes.php` fails and the script terminates.

Any suggestions on what I'm doing wrong?


Solution

  • this should work:

    cd /D "D:\xampp\htdocs\MyApp\App\Cronjobs\"
    "D:\xampp\php\php.exe" -f CronjobBase.php MyCronJob
    

    OR:

    D:
    cd "D:\xampp\htdocs\MyApp\App\Cronjobs\"
    "D:\xampp\php\php.exe" -f CronjobBase.php MyCronJob