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

Is there an easy way to do this?

Was it helpful?


You must use sc.exe. Visit for the details. Then just use php.exe yourscriptname as a command line for the service to execute


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.)

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()

        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)

The requisite batch file:

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

This is possibly a question for or

This might offer some help about the service

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top