Thursday, April 23rd, 2009 | Author:

Hey All,

So I have an account over at JaguarPC and I love it a lot there, they support everything I need and help me with all that I ask. However, there seems to be a snafu/caveat what have you to running RoR (Ruby on Rails) apps at JPC. cPanel runs RoRs apps using a mongrel instance, this is fine and it works….but sometimes it dies and needs to be restarted. Highly annoying….I run my redmine installation off of one of these mongrel instances and I really can’t have it be down. So….I devised this little gem below to check if your mongrel instance is down, if so to restart it.

To use the script throw it in your cPanel root and add the following line to your crontab:


*/5 * * * * php ~/reboot.php

Be sure to update the command above to reflect what you named the script…..

Hopefully someone else finds it useful and if you need help with the script comment!

cPanel Ror Mongrel Reboot


<?php
/*
 *
 * cPanel Mongrel Rebooter - v0.1
 * Author: Robert Navarro <[email protected]>
 *
 */

/*
 * The siteCheck function has the following parameters
 *
 * $url - This is the url of the application you want to check
 * $searchString - This is the erroneous string you want search the site for
 * $appPath - This is the full path to your cPanel RoR app installation
 * $appPort - This is the port that your cPanel RoR app runs on
 *
 * You can find the application port by running:
 * # ps aux |grep mongrel |grep <your username>
 *
 */
siteCheck("http://<my redmine install>","502 Proxy Error","~/Sites/<my redmine install>/","12001");

function siteCheck($url,$searchString,$appPath,$appPort) {
 // open the remote URL for reading
 $fp = fopen($url, 'r') or die('Unable to open file '.$off_site.' for reading');

 $buffer = '';

 // read in chunks
 while(!feof($fp))
 {
 $buffer .= fgets($fp, 4096);
 }

 // strip HTML but leave the tags you want
 $output = strip_tags($buffer,'<a>
<ul>
	<li>
<h2>
<h1><span>');

 // convert all whitespace, tabs and newlines into one single whitespace
 $output = preg_replace('/\s+/', ' ', $output);

 if(strstr($output,$searchString)) {
 restart($appPath,$appPort);
 }
}

function restart($path,$port) {
 exec("cd $path;mongrel_rails start -p $port -d -e production -P $path/log/mongrel.pid");

}
?>

Category: Development
You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

5 Responses

  1. 1
    Wei Feng 

    Hi, Thanks for your idea!

    I changed your script a little bit – use HTTP status code to determine if the website is alive or not.

    To achieve this, curl is hired and here is the code:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $website_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_HEADER, true);

    $result = curl_exec($ch);

    // Get HTTP status code
    if (preg_match(“|^HTTP/1.1 (\d{3})|”, $result, $m)) {
    $http_status = $m[1];
    } else {
    die(“Invalid HTTP result: $result\n”);
    }

    Then you can use $http_status: 302 – website is running; 502 (in my case 503) – web site needs restart

  2. That works perfectly fine as well! Thanks for your feedback Wei, much appreciated.

  3. 3
    Sdanektir 

    I wanted to ask, is there any chance for a modified version of the directory listing script, with an iphone-stylish like design?

  4. What kind of design were you looking for? This is really a console script not so much an actual “web” script.

  5. It is also easy to backup and transfer all your websites from one server to another server if you have cPanel installed,*: