简体   繁体   English

在 wamp 服务器中运行一个 php 脚本作为后台进程

[英]Run a php script as a background process in wamp server

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. Wamp 服务器安装在 Windows 7 PC 中。 These scripts are already reside in separate folder in the www root directory.这些脚本已经位于 www 根目录中的单独文件夹中。

Apache Version :2.2.8阿帕奇版本:2.2.8
PHP Version :5.2.6 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.由于这不是 unix 平台,我不能使用nohup php script.php > /dev/null &命令来完成这项工作。 I'm looking for similar kind of command or method which works in wamp server windows platform.我正在寻找在 wamp 服务器 Windows 平台中工作的类似命令或方法。

Can anyone explain the steps I need to be taken to do this task?任何人都可以解释我需要采取的步骤来完成这项任务吗?

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.php that is located in folder named "feed" and we pass 2 parameters (database name and some other second parameter).在上面的示例中,我们启动位于名为“feed”的文件夹中的脚本handleFeed.php传递 2 个参数(数据库名称和其他一些第二个参数)。

  1. create a batch file to run your php script using php executable "C:\\wamp\\php\\php.exe C:\\wamp\\www\\index.php"创建一个批处理文件以使用 php 可执行文件“C:\\wamp\\php\\php.exe C:\\wamp\\www\\index.php”运行你的 php 脚本
  2. add this batch file in Scheduled Task in Windows control panel.在 Windows 控制面板的计划任务中添加此批处理文件。

This is what I did:这就是我所做的:

  1. PHP file PHP文件

    <?php my code goes here ?>

    *Note if you are using HTTP API/CURL in CLI use dl("php_curl.dll"); *注意,如果您在 CLI 中使用 HTTP API/CURL,请使用dl("php_curl.dll");

    this loads curl into cli这将卷曲加载到 cli

  2. Now I added PHP to windows path variable, this can be done from My computer, properties, advanced settings, environment variables, new现在我将 PHP 添加到 windows 路径变量,这可以从我的电脑、属性、高级设置、环境变量、新

  3. Next I created a .bat file, simply open a notepad & type code below and save as myfile.bat接下来我创建了一个 .bat 文件,只需打开一个记事本并在下面输入代码并保存为 myfile.bat

     @ECHO OFF php -fd:\\wamp\\www\\V3\\task.php

    This site might help you on .bat file syntax.该站点可能会帮助您了解 .bat 文件语法。

  4. Now create a new scheduled task on windows & call the above .bat file as source,现在在 Windows 上创建一个新的计划任务并调用上面的 .bat 文件作为源,

在此之间: http://php.net/manual/en/install.windows.commandline.php ,并使用“at”实用程序,您应该能够让它工作。

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 命令的信息:

  1. Win - R-R
  2. type cmd输入cmd
  3. type help start输入help start

/// 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);
?>

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

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