0

For an iOS Push Notification server, I am implementing a web service that checks a feed on the net for a particular price.

Therefore I need my PHP to keep checking a price (every 20 seconds or so) and check values.

I was wondering (forgive my ignorance I just started with PHP today) is the way people do this a cronjob? Or is there some special way to fire a php script that runs until it's killed and repeats a task?

Thanks! John

4
  • 1
    the minimum frequency of a cron job is per minute, for 20 seconds i would write a daemon. Commented Aug 13, 2013 at 20:17
  • is that really how its done? No simpler way in PHP directly? Commented Aug 13, 2013 at 20:18
  • its probably done 50 different ways. but for that interval that's how i would do it. perhaps php is not the appropriate language for this. Commented Aug 13, 2013 at 20:19
  • Thanks for the info, what else can be used python? Commented Aug 13, 2013 at 20:22

2 Answers 2

1

If PHP was your preferred route, a simple script such as the following can be set to run indefinitely in the background (name this grabber.php):

#!/usr/bin/php
<?php
do {
    // Grab the data from your URL
    $data = file_get_contents("http://www.example.com/data.source");

    // Write the data out somewhere so your push notifications script can read it
    file_put_contents("/path/to/shared/data.store", $data);

    // Wait and do it all over again
    sleep(20);
} while (true);

And to start it (assuming you're on a unixy OS):

$ chmod u+x grabber.php
$ ./grabber.php > /path/to/a/file/logging/script/output.log 2>&1 &

That & at the end sends the process to run in the background.

PHP is probably overkill for this however, perhaps a simple bash script would be better:

#!/bin/bash
# This downloads data and writes to a file ('data-file')
doWork () {
    data=$(curl -L http://www.example.com/data.source)
    echo $data > data-file
    sleep 20
    doWork
}

# Start working
doWork

$ chmod u+x grabber.sh
$ ./grabber.sh > /path/to/logger.log 2>&1 &
Sign up to request clarification or add additional context in comments.

1 Comment

great stuff! thanks! I need PHP to set up a secure connection to the apple push notification server and send a json string. sockets etc... can't use bash.
1

That is possible by setting up a cron jobs on your server.

  1. Login to your web hosting e.g cpanel create a new cron job and add the path to the php file that you want to run. e.g php /home/[your username]/public_html/rss/import_feeds.php. There is field where you can input the number of minutes would you like the php script to run.

Run a PHP file in a cron job using CPanel

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.