Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a php script with a .bat file

I need to run a php script at midnight every night on my server. On a linux system I'd set up a cron job, but I'm stuck with a windows system.

I know I have to set up a task using the windows task scheduler, and that the task will need to run a .bat file which in turn will run the php file, but I'm stuck trying to write the .bat file.

What I currently have is:

@echo off
REM this command runs the nightly cron job
start "C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php

But when I try to manually run the .bat file to test it, I get a windows alert saying

"Windows cannot find '-f'. Make sure you typed the name correctly, and then try again.

What have I missed?

like image 370
evilscary Avatar asked Aug 28 '13 13:08

evilscary


People also ask

How do I run a batch file in PHP?

Use single quotes like this $str = exec('start /B Path\to\batch. bat'); The /B means the bat will be executed in the background so the rest of the php will continue after running that line, as opposed to $str = exec('start /B /C command', $result); where command is executed and then result is stored for later use.

How do I run a PHP script?

If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.


2 Answers

The START command optionally accepts a title for the created window as its first argument; in this case, it thinks that C:\Program Files (x86)\PHP\v5.3\php.exe is the title to display and -f (the second argument) is the executable you want to run.

You can therefore fix this by providing a placeholder title, e.g.

start "email reminder task" "C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php

Or, preferably, you can ditch the START command altogether (you aren't using any of its unique facilities) and just run PHP directly:

"C:\Program Files (x86)\PHP\v5.3\php.exe" -f C:\inetpub\wwwroot\sitename\crons\reminder-email.php
like image 136
Jon Avatar answered Oct 12 '22 23:10

Jon


Actually, you don't even need a batch-file. You can run the php-script from the task scheduler.

Just let the task scheduler run php.exe and set the location of the php-file as the argument of the task.

like image 42
JelleLampaert Avatar answered Oct 12 '22 23:10

JelleLampaert