AKrush95
AKrush95

Reputation: 1441

Run PHP script constantly as windows service

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.

Upvotes: 1

Views: 3721

Answers (3)

ssarljames
ssarljames

Reputation: 21

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

Upvotes: 2

AKrush95
AKrush95

Reputation: 1441

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

Upvotes: 0

Nick Tsarev
Nick Tsarev

Reputation: 103

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.

Upvotes: 0

Related Questions