简体   繁体   English

Windows上的计划任务运行PHP文件

[英]Scheduled task on windows to run a PHP file

We distribute a PHP application that requires a scheduled task to be created to run a PHP script. 我们分发一个PHP应用程序,它需要创建一个计划任务来运行PHP脚本。 I know how to do this. 我知道怎么做。 What I am trying to work out is the way to make this as simple as possible for the admin installing the software to set this up (ideally almost one click). 我想要解决的是让管理员安装软件来设置它的方式尽可能简单(理想情况下几乎只需点击一下)。

The problems: 问题:

i) We don't know where PHP is installed ii) We don't know where they have installed the file that we need to run. i)我们不知道PHP的安装位置ii)我们不知道他们在哪里安装了我们需要运行的文件。

My initial plan was to create a .bat file contains the path to PHP (so they would need to edit this file) and this loads our PHP script so is what they would need to create a scheduled task to run. 我最初的计划是创建一个.bat文件,其中包含PHP的路径(因此他们需要编辑此文件),这会加载我们的PHP脚本,因此他们需要创建一个计划任务来运行。 Then there would be a second .bat file that would create the scheduled task itself (not entirely sure how to do that part). 然后会有第二个.bat文件,它将自己创建计划任务(不完全确定如何执行该部分)。

Does this seem like the best approach? 这看起来像是最好的方法吗? If so, any pointers on how to create a scheduled task using a .bat file - I can't find this information. 如果是这样,任何关于如何使用.bat文件创建计划任务的指针 - 我找不到此信息。

There is command "at" you can use in your .bat files to create scheduled task however it needs to be run as an admin. 您可以在.bat文件中使用命令“at”创建计划任务,但需要以管理员身份运行。

Edit: schtasks does the same and may be used without admin rights. 编辑:schtasks执行相同操作,可以在没有管理员权限的情况下使用。

You may search for php.exe, but it's little tricky - basically you need to use dir with /s parameter. 你可以搜索php.exe,但这有点棘手 - 基本上你需要使用带/ s参数的dir。

Why not inno setup for eg? 为什么不设置例如?

To run PHP scripts from bat files you can simply create ".bat" files and add your file path. 要从bat文件运行PHP脚本,您只需创建“.bat”文件并添加文件路径即可。 Below is example: 以下是示例:

  1. create "your_file_name.bat" 创建“your_file_name.bat”
  2. open in editplus or notepad and type: 在editplus或记事本中打开并输入:

rename your filename in below example 在下面的示例中重命名您的文件名

"D:\Program Files (x86)\EasyPHP-5.3.8.0\php\php.exe" "D:\Program Files (x86)\EasyPHP-5.3.8.0\www\project_name\your_script_name.php" >> log_new.txt
rem "D:\Program Files (x86)\EasyPHP-5.3.8.0\php\php.exe" your_script_name.php
pause
  • That's it, you are done. 就是这样,你完成了。
  • log.txt will log whatever output or error generated by php file. log.txt将记录php文件生成的任何输出或错误。

Hope this will solve your issue :) 希望这会解决你的问题:)

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

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