2010-01-27 120 views

回答

1

您必須使用sc.exe。詳情請訪問http://support.microsoft.com/kb/251192。 就用php.exe的yourscriptname作爲一個命令行的服務執行

+0

我不認爲這會工作,因爲這個exe有啓動時返回。這就是爲什麼你在上面的csharp程序中有OnStart方法的原因。 – 2010-01-27 11:04:35

1

如果你不介意讓你的手弄髒一點csharp,這裏是一個帶有一個windows應用程序的網址。它設置一個計時器,每隔幾秒鐘執行一次批處理文件(即腳本)。只有當您的腳本執行任務然後退出時纔會有效。 (標記爲社區維基,因爲這不是我的代碼。我在這裏複製所有代碼的情況下,鏈接網站在未來去死。)

http://www.akchauhan.com/create-windows-service-to-schedule-php-script-execution/

這裏的鏈接文章中提到的代碼。

C#的服務:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Diagnostics; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 
using System.Timers; 

namespace MyNewService 
{ 
    public partial class MyNewService : ServiceBase 
    { 
     private Timer syncTimer = null; 

     public MyNewService() 
     { 
      InitializeComponent(); 
     } 

     protected override void OnStart(string[] args) 
     { 
      syncTimer = new Timer(); 
      this.syncTimer.Interval = 180000; 
      this.syncTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.syncTimer_Tick); 
      syncTimer.Enabled = true; 
     } 

     protected override void OnStop() 
     { 
      syncTimer.Enabled = false; 
     } 

     private void syncTimer_Tick(object sender, EventArgs e) 
     { 
      System.Diagnostics.Process.Start(@"C:\xampp\htdocs\task.bat"); 
     } 
    } 
} 

的必要的批處理文件:

@echo off 
cd\ 
set path=C:\xampp\php; 
cd "C:\xampp\htdocs" 
php import.php 
exit 
相關問題