1

I have a PHP script that polls a database, checks for new entries, and, if a new entry is found, sends out a push notification to iOS devices.

I currently have the php script set up so if you refresh the page constantly, it does what I want. I could also set up a php or javascript refresh and just have the page open, but that wouldn't be very effective if I'm working with a remote server and can't tell what programs are open or when it restarts.

I'm thinking that a windows service would be the best solution to my issue, but I'm not sure how I can compile the php script into a windows service.

Does anybody have any suggestions? Worst case, I guess I could just write everything in C# and make a windows service out of that, but I'm sure there's an easier way.

John Conde
  • 217,595
  • 99
  • 455
  • 496
AKrush95
  • 1,381
  • 6
  • 18
  • 35

3 Answers3

2

In Windows

You can use nssm.exe (Non-Sucking Service Manager). Download it here https://nssm.cc/

First, create your php script that polls a database:

<?php

/// script.php

while(true){
   //do some stuff here

   sleep(5); //wait for 5 seconds 
}

?>

Then in console, enter this command

nssm.exe install <service-name>

After that, In the Nssm service installer

  1. In the path field, write path of your php.exe
  2. In start up directory, write the location of your php script or your working directory
  3. Lastly, in arguments field, write script.php

Then click the button install service. You can now check your service in windows service

ssarljames
  • 21
  • 3
0

Try to use Windows Task Scheduler. It's not so powerful,like cron on unix-systems, but it will helps you. Here you can to find little more info about it How to run a PHP file in a scheduled task (Windows Task Scheduler)

If you have(want) access to your script from web, you can use Windows Task Scheduler with wget, for example create new task with next path: wget.exe -O - -q -t 1 http://www.your-site.com/cron.php. Of cource, you must install wget to your local server.

Community
  • 1
  • 1
Nick Tsarev
  • 103
  • 1
  • 1
  • 10
  • But how often can task scheduler run this task? I'd like for it to be able to run at least twice a second. – AKrush95 Oct 27 '13 at 01:46
  • It depends from version of your operating system. You can create test trigger and look a minimal time interval of repeat of the task. If repeat time isn't enough you, you can create two or more triggers. – Nick Tsarev Oct 27 '13 at 07:07
0

My solution was to use the program FireDaemon (AlwaysUp is an alternative). I installed PHP manually to C:\PHP and set the task to run

C:\PHP\php.exe C:\pathtophp\file.php
AKrush95
  • 1,381
  • 6
  • 18
  • 35