You are only in a hurry till you get to the front of the queue

Wednesday, 14 January 2015

Make a cron job with IFTTT - Barattalo

Make a cron job with IFTTT - Barattalo:



'via Blog this'



Cron is a software utility, a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs to run periodically at fixed times or dates.
Sometimes, as a developer, I don’t have access to the server at a deep level and, for example on some basic shared servers, I can’t create cron jobs. A trick to create a cron jobs without server cron program is to use an external service to call your scripts.
For example, you can make it with IFTTT a famous web site that lets you make very astonishing things mixing web services from different sites.
IFTTT works with triggers that are fired on events and make actions.
Some examples: if there is a new item in a rss feed, then send me an email. If tomorrow weather forecast is rain, post a tweet with “take umbrella!”. If I upload a new photo on Instagram with hashtag #Facebook post it to my Facebook profile.
…and many more.
One great missing action is the action of calling a custom url. I don’t know why since this could help many developers to build even more custom actions with IFTTT, such as a cron job that call a php that performs some updates in the database.
Fortunately there is a trick to set up a cron job with IFTTT, throught the Dropbox channel.
So you must connect Dropbox to IFTTT by adding the Dropbox channel and then use the “Add file from url” action to call your php. Mixing this action with the date/time trigger can build a cronjob like cron on unix system.
Here is how you can do it within IFTTT panel. Remember you need to activate the Date & Time channel to use the trigger and also the Dropbox channel to use the save url action that calls your php.
Content of iftttcron.php called for “fake” download in Dropbox channel, the php send headers to emulate a download of some txt data (you will find these files inside your dropbox account and sometimes you have to delete them):
1
2
3
4
5
6
7
header('HTTP/1.1 200 OK');
header('Content-type: text/plain');
header('Content-disposition: attachment; filename=delete-me.txt');
echo "ok";
// --------------- action -------
// do something...
mail("youremail@yourdomain.com","Hello new!","Send at ".date("Y-m-d H:i:s").". bye.","From:info@yourdomain.com");
UPDATE 11/17/2013
I’ve noticed that IFTTT makes multiple calls, not only one. This results in sending multiple emails and that’s BAD!
So, here is a trick to create a mini caching system for your iftttcron.php file. Before executing the action, check if there is a fresh cached version, if the cached version is old make actions and create the new cache file:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
header('HTTP/1.1 200 OK');
header('Content-type: text/plain');
header('Content-disposition: attachment; filename=delete-me.txt');
 
include("demo/minibots/minibots.class.php");
 
$output = "ok";
 
$cache_file_name = "iftttcron.txt";
$age_in_minutes = round((time() - @filemtime($cache_file_name))/60);
if ($age_in_minutes < 60) {
    // output from old cached file
    echo file_get_contents($cache_file_name);
} else {
    // file older than 1 hour
    // --------------- action -------
    // do something...
    mail("youremail@yourdomain.com","Hello new!","Send at ".date("Y-m-d H:i:s").". bye.","From:info@yourdomain.com");
     
    //
    // overwrite old cached file
    // with new data
    $f = fopen($cache_file_name,"w");
    fwrite($f,$str);
    fclose($f);
 
    //
    // output results
    echo $output;
 
}
This should prevent multiple emails.

1 comment :

  1. Bluehost is definitely the best hosting provider with plans for any hosting needs.

    ReplyDelete

Thank you for taking the time to comment. Your opinion is important and of value and we appreciate the positive feedback! If you are "Negative Nancy" then please do us, and humanity, a favor and piss off.

Total Pageviews

Google+ Followers

Interesting pages

Blog Archive

Popular Posts

Recent Comments

Rays Twitter feed

Ads

Web sites come and go and information is lost and therefore some pages are archived. @rayd123 . Powered by Blogger.