CRONTAB – Predefined scheduling definitions

Predefined scheduling definitions

There are several special predefined values which can be used to substitute the CRON expression.

Entry Description Equivalent To
@yearly (or @annually) Run once a year, midnight, Jan. 1st 0 0 1 1 *
@monthly Run once a month, midnight, first of month 0 0 1 * *
@weekly Run once a week, midnight on Sunday 0 0 * * 0
@daily Run once a day, midnight 0 0 * * *
@hourly Run once an hour, beginning of hour 0 * * * *
@reboot Run at startup

 

*    *    *    *    *  command to be executed
┬    ┬    ┬    ┬    ┬
│    │    │    │    │
│    │    │    │    │
│    │    │    │    └───── day of week (0 - 6) (0 is Sunday, or use names)
│    │    │    └────────── month (1 - 12)
│    │    └─────────────── day of month (1 - 31)
│    └──────────────────── hour (0 - 23)
└───────────────────────── min (0 - 59)
Posted in Code Examples, PHP | Leave a comment

PHP Site Monitor Script (with email alerts or text messages.)

<?php
// check list of URLs (select URLs with small traffic - static content)
url_test('http://www.fabiodemelo.com');
url_test('http://www.demelos.com');

//Email
//If you like to receive a text message check for the web. for example at&t will be number@txt.att.net.
function url_test($url, $email_admin='your@email.address'){
// set timeout
$timeout = 10;
// init url session and set curl options
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);
$http_respond = curl_exec($ch); // execute http request
// remove all html tags from the http response
$http_respond = trim(strip_tags($http_respond));
// get status from the http response
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// returned http code should be 200 otherwise there is a problem
if ($http_code != 200){
// prepare tested server name (component parameter added in PHP 5.1.2)
$http_server = parse_url($url, PHP_URL_HOST);
// and who was tester
$hostname = trim(`/bin/hostname`);
// get an error message from the URL request
$http_error = trim(curl_error($ch));
// prepare e-mail message
$email_message = "TEST URL: $url";
if ($http_code) $email_message .= "\n\nHTTP CODE: $http_code";
if ($http_respond) $email_message .= "\n\nHTTP RESPOND:\n$http_respond";
if ($http_error) $email_message .= "\n\nHTTP ERROR:\n$http_error";
// prepare e-mail header
$email_header = "Content-Type: text/plain; charset=\"iso-8859-2\"\n".
"Subject: http://$http_server problem!\n".
"From: monitor@$hostname\n".
"X-Priority: 1\n".
"Priority: Urgent\n".
"Importance: high";
// send e-mail message
error_log($email_message, 1, $email_admin, $email_header);
}
// close url session
curl_close($ch);
}
?>

I run monitor script every five minutes and here is my cron entry:

# PHP Web server monitor
*/5 * * * * /usr/bin/php /usr/local/scripts/monitor.php
Posted in Code Examples, PHP | Leave a comment

Php Curl

If you want to write a sort of php wrapper to include the results of another http(s) request maybe pointing to a totally different site or just different code (mod_perl with HTML::Mason, in my case) into a php based layout, and just pass-thru all GET and POST variables to the sub-request, the following snippet can be used. Note there is no error handling, so this is subject to the underlying application.

<?php
$ch = curl_init($sub_req_url);
$encoded = '';
// include GET as well as POST variables; your needs may vary.
foreach($_GET as $name => $value) {
  $encoded .= urlencode($name).'='.urlencode($value).'&';
}
foreach($_POST as $name => $value) {
  $encoded .= urlencode($name).'='.urlencode($value).'&';
}
// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);
curl_setopt($ch, CURLOPT_POSTFIELDS,  $encoded);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_exec($ch);
curl_close($ch);
?>
Posted in Code Examples, PHP | Leave a comment