简体   繁体   English

使用.bat文件运行php脚本

[英]Running a php script with a .bat file

I need to run a php script at midnight every night on my server. 我需要在我的服务器上每晚午夜运行一个php脚本。 On a linux system I'd set up a cron job, but I'm stuck with a windows system. 在Linux系统上,我设置了一个cron作业,但我坚持使用Windows系统。

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. 我知道我必须使用Windows任务调度程序设置一个任务,并且该任务需要运行.bat文件,而该文件又会运行php文件,但我不得不尝试编写.bat文件。

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 但是,当我尝试手动运行.bat文件进行测试时,我收到一个Windows警报说

"Windows cannot find '-f'. Make sure you typed the name correctly, and then try again. “Windows无法找到'-f'。请确保正确输入名称,然后重试。

What have I missed? 我错过了什么?

The START command optionally accepts a title for the created window as its first argument; START命令可选地接受所创建窗口的标题作为其第一个参数; 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. 在这种情况下,它认为C:\\Program Files (x86)\\PHP\\v5.3\\php.exe是要显示的标题, -f (第二个参数)是您要运行的可执行文件。

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: 或者,最好,你可以完全抛弃START命令(你没有使用它的任何独特功能),只需直接运行PHP:

"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. 您可以从任务计划程序运行php脚本。

Just let the task scheduler run php.exe and set the location of the php-file as the argument of the task. 让任务调度程序运行php.exe并将php文件的位置设置为任务的参数。

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. PS。 Putting Apache,MySQL or PHP in Program Files is a bad idea. 将Apache,MySQL或PHP放在Program Files是个坏主意。 Dont use windows folders with spaces in their names. 不要使用名称中带空格的Windows文件夹。

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. 可悲的是,这花了将近10个小时来搞清楚。 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). 某些协议(如WinRM和PSEXEC)必须通过登录的用户帐户,无论提供的凭据如何(或者PHP会覆盖您键入的内容)。 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). 无论如何,要获得PSEXEC,WinRM或只是启动连接到远程计算机的批处理文件,您需要将IIS作为帐户更改为具有这些资源(服务帐户)权限的用户。 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. PHP是安全的,因此不容易被黑客攻击,您必须通过指定run as account来覆盖其安全性。 Not the same thing as your app pool account - although your IIS credentials can use your app pool account. 与您的应用池帐户不同 - 尽管您的IIS凭据可以使用您的应用池帐户。

Try like this guys! 试试这个家伙!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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