windows 在 wamp 服务器中运行一个 php 脚本作为后台进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4231539/
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
Run a php script as a background process in wamp server
提问by Lahiru Ruhunage
I have two php scripts that need to be run as continuous back ground processes in WAMP server.
我有两个 php 脚本需要在 WAMP 服务器中作为连续的后台进程运行。
Wamp server is installed in window 7 PC. These scripts are already reside in separate folder in the www root directory.
Wamp 服务器安装在 Windows 7 PC 中。这些脚本已经位于 www 根目录中的单独文件夹中。
Apache Version :2.2.8
PHP Version :5.2.6
Apache 版本:2.2.8
PHP 版本:5.2.6
Since this is not a unix platform I can't use
nohup php script.php > /dev/null &
command to do this job. I'm looking for similar kind of command or method which works in wamp server windows platform.
由于这不是 unix 平台,我无法使用
nohup php script.php > /dev/null &
命令来完成这项工作。我正在寻找在 wamp 服务器 Windows 平台中工作的类似命令或方法。
Can anyone explain the steps I need to be taken to do this task?
任何人都可以解释我需要采取的步骤来完成这项任务吗?
回答by Imran Naqvi
- create a batch file to run your php script using php executable "C:\wamp\php\php.exe C:\wamp\www\index.php"
- add this batch file in Scheduled Task in Windows control panel.
- 创建一个批处理文件以使用 php 可执行文件“C:\wamp\php\php.exe C:\wamp\www\index.php”运行你的 php 脚本
- 在 Windows 控制面板的计划任务中添加此批处理文件。
回答by Dejv
Simply use this function. It works under both OSs (Windows and Linux):
只需使用此功能。它适用于两种操作系统(Windows 和 Linux):
function execInBackground($cmd){
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}else{
exec($cmd . " > /dev/null &");
}
}
Here is an easy example of how to use the function:
以下是如何使用该函数的简单示例:
execInBackground('php feed/handleFeed.php db_name '.$second_param);
In above example, we start script handleFeed.phpthat is located in folder named "feed"and we pass 2 parameters(database name and some other second parameter).
在上面的示例中,我们启动位于名为“feed”的文件夹中的脚本handleFeed.php并传递 2 个参数(数据库名称和其他一些第二个参数)。
回答by jcomeau_ictx
Between this: http://php.net/manual/en/install.windows.commandline.php, and using the "at" utility, you ought to be able to get it working.
在此之间:http://php.net/manual/en/install.windows.commandline.php,并使用“at”实用程序,您应该能够让它工作。
回答by Alex
You can use "start" before start background script. Example:
您可以在启动后台脚本之前使用“启动”。例子:
create cron.cmd with
创建 cron.cmd
start /B php.exe "path to your script 1"
start /B php.exe "path to your script 2"
You can show man about the start command:
您可以向 man 显示有关 start 命令的信息:
- Win-R
- type
cmd
- type
help start
- Win——R
- 类型
cmd
- 类型
help start
回答by charles
This is what I did:
这就是我所做的:
PHP file
<?php my code goes here ?>
*Note if you are using HTTP API/CURL in CLI use
dl("php_curl.dll");
this loads curl into cli
Now I added PHP to windows path variable, this can be done from My computer, properties, advanced settings, environment variables, new
Next I created a .bat file, simply open a notepad & type code below and save as myfile.bat
@ECHO OFF php -f d:\wamp\www\V3\task.php
This sitemight help you on .bat file syntax.
Now create a new scheduled task on windows & call the above .bat file as source,
PHP文件
<?php my code goes here ?>
*请注意,如果您在 CLI 中使用 HTTP API/CURL
dl("php_curl.dll");
这将卷曲加载到 cli
现在我将 PHP 添加到 windows 路径变量,这可以从我的电脑、属性、高级设置、环境变量、新
接下来我创建了一个 .bat 文件,只需打开一个记事本并在下面输入代码并保存为 myfile.bat
@ECHO OFF php -f d:\wamp\www\V3\task.php
该站点可能会帮助您了解 .bat 文件语法。
现在在 Windows 上创建一个新的计划任务并调用上面的 .bat 文件作为源,
回答by Rutvik kakadiya
/// we can execute PHP script file as a background process in the windows Xampp server using the below code.
/// 我们可以使用以下代码在 windows Xampp 服务器中执行 PHP 脚本文件作为后台进程。
<?php
exec('C:\xampp\php\php.exe C:\xampp\htdocs\project\bg_script.php);
?>