使用 .bat 文件运行 php 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18488946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:38:31  来源:igfitidea点击:

Running a php script with a .bat file

phpwindowsbatch-file

提问by evilscary

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.

我需要在每晚午夜在我的服务器上运行一个 php 脚本。在 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?

我错过了什么?

采纳答案by Jon

The STARTcommand 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.exeis the title to display and -f(the second argument) is the executable you want to run.

START命令可选择接受所创建窗口的标题作为其第一个参数;在这种情况下,它认为这C:\Program Files (x86)\PHP\v5.3\php.exe是要显示的标题,-f(第二个参数)是您要运行的可执行文件。

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 STARTcommand 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

回答by JelleLampaert

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 文件的位置设置为任务的参数。

回答by RiggsFolly

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 Filesis a bad idea. Dont use windows folders with spaces in their names.

附注。放入 Apache、MySQL 或 PHPProgram Files是个坏主意。不要使用名称中带有空格的 Windows 文件夹。

回答by Interbot Development Team

How about this?

这个怎么样?

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

回答by Bobby T

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.

可悲的是,这花了将近 10 个小时才弄明白。某些协议(例如 WinRM 和 PSEXEC)必须传递登录的用户帐户,而不管提供的凭据如何(该凭据或 PHP 会覆盖您输入的任何内容)。无论如何,要获得 PSEXEC、WinRM 或只是启动连接到远程计算机的批处理文件,您需要将 IIS 运行帐户更改为有权访问这些资源的用户(服务帐户)。我意识到这是一个巨大的安全漏洞,但这是设计使然。PHP 是安全的,因此它不会被轻易破解,您必须通过指定运行帐户来覆盖它们的安全性。与您的应用程序池帐户不同 - 尽管您的 IIS 凭据可以使用您的应用程序池帐户。

回答by Shinu K Narayanan

Try like this guys!

像这样的家伙尝试!

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