views:

50

answers:

3

I need a PHP script to run as a service in windows.

Is there an easy way to do this?

A: 

You must use sc.exe. Visit http://support.microsoft.com/kb/251192 for the details. Then just use php.exe yourscriptname as a command line for the service to execute

alemjerus
I don't think this would work since the exe has to return when starting. That's why you have the OnStart method in the csharp program above.
Tim Coker
A: 

This is possibly a question for http://superuser.com/ or http://serverfault.com/

This might offer some help about the service http://support.microsoft.com/kb/251192

It's been a while since I've worked with windows, but you might be able to setup a batch file to run as a service.

woody993
+1  A: 

If you don't mind getting your hands dirty with a little csharp, here's an url with a shell application that is a windows service. It sets a timer that executes a batch file (ie, your script) every so many seconds. Would only work if your script does a task then exits. (Marking as community wiki since this isn't my code. I'm copying all code here in case the linked site goes dead in the future.)

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

Here's the code mentioned in the linked article.

C# for the service:

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");
        }  
    }
}

The requisite batch file:

@echo off  
cd\  
set path=C:\xampp\php;  
cd "C:\xampp\htdocs"  
php import.php  
exit  
Tim Coker