1
votes

I have created an application in openshift. I have a cron which should run every minute since it is placed in minutely folder inside cron. But it never runs. Its a php script which hits a url using curl. Any idea

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

I created this script and placed it inside minutely folder in .openshift/crons folder. Then I restarted my application. But it doesn't work. Any idea?

2
can you post the script? - lootsch
@lootsch: the script is the posted code - SanketR

2 Answers

5
votes

You will need two files.

1.: THE CRON FILE

It is a script, that will execute your PHP script. You need to place it in the minutely folder. Let's name it "crontest.sh", so the full path will be this, where the 000000000000000000000000 is your own OPENSHIFT_APP_UUID:

/var/lib/openshift/000000000000000000000000/app-root/runtime/repo/.openshift/cron/minutely/crontest.sh

The file contains only this line:

php $OPENSHIFT_REPO_DIR/php/crontest.php

2.: THE PHP FILE

It is your PHP script, that will be executed every minute by your Cron script. You need to place in the same folder, that you have specified in your Cron file. Let's name it "crontest.php", so the full path will be this, where the 000000000000000000000000 is your own OPENSHIFT_APP_UUID:

/var/lib/openshift/000000000000000000000000/app-root/runtime/repo/php/crontest.php

The file contains your PHP script, e.g. this will make a file named "crontest.txt" showing up next to your PHP script, containing as many "1" as the number of the passed minutes is:

<?php

    file_put_contents(getenv('OPENSHIFT_REPO_DIR').'php/crontest.txt', '1', FILE_APPEND);

?>

To answer SanksR's specific question, the PHP file will contain the code below in the "app-root/runtime/repo/php/crontest.php" file, while the "app-root/runtime/repo/.openshift/cron/minutely/crontest.sh" will contain this: "php $OPENSHIFT_REPO_DIR/php/crontest.php".

<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>
1
votes

You have to write a shell/bash script and place it in the minutely folder. This script has to run your php file. It could look like:

myscript.sh:

#!/bin/bash
export PHP=/usr/local/zend/bin/php ;
$PHP my-curl-cron.php

(don't forget to make it executable: chmod +x myscript.sh) I recommend to read this article along with this tutorial.