简体   繁体   中英

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?

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, eg

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

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.

Can I suggest a small change.

echo off
REM This adds the folder containing php.exe to the path
PATH=%PATH%;C:\Program Files (x86)\PHP\v5.3

REM Change Directory to the folder containing your script
CD C:\inetpub\wwwroot\sitename\crons

REM Execute
php reminder-email.php

PS. Putting Apache,MySQL or PHP in Program Files is a bad idea. Dont use windows folders with spaces in their names.

How about this?

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

Sadly this took close to 10 hours to figure out. Some protocols such as WinRM and PSEXEC must pass the logged in user account regardless of the credentials supplied (that or PHP overrides whatever you type in). At any rate, to get PSEXEC, WinRM or just kicking off batch files that connect to remote computers, you will need to change the IIS run as account to a user with rights to those resources (service account). I realize this is a huge security hole, but that is by design. PHP is secure so that it can't be hacked easily, you have to override their security by specifying a run as account. Not the same thing as your app pool account - although your IIS credentials can use your app pool account.

Try like this guys!

cd E:\xampp\htdocs\my-project
E:
php artisan schedule:run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM