Problems with cron and Runway (with a solution)

  • For the last two Runway installs I've had issues getting cron to then run on my server setup (in order to schedule the backups to Amazon S3). I now know the cause, but I'm unsure if its specific to my server hosting setup. I also have a solution, so I have decided to publish the info here for others. If you have the same issue and solved it a different way, I'd love to hear.


    On a Runway install, the config.php contains the following code at the top ...


    PHP
    1. <?php
    2.     switch($_SERVER['SERVER_NAME']) {
    3.         case 'www.mysite.com':
    4.             include(__DIR__.'/config.www-mysite-com.php');
    5.             break;
    6.         default:
    7.             include('config.production.php');
    8.             break;
    9.     }


    The file config.www-mysite-com.php contains the sitepath, schedule_secret and database settings. The file config.production.php contains nothing except for an opening <?php statement. This all works perfectly when using Runway with http or https because the variable of $_SERVER['SERVER_NAME'] has a value.


    When using cron to set up an hourly job, I use a statement like this ...


    Code
    1. /opt/cpanel/ea-php72/root/usr/bin/php /home/mysite/public_html/perch/core/scheduled/run.php xxxxxxxxxx


    The cron job runs ok but doesn't do anything. After digging around I see this error ...

    'PHP Parse error: syntax error, unexpected end of file in /home/mysite/public_html/perch/config/config.production.php on line 1'


    My hosting company tells me that the cause is that the variable of $_SERVER['SERVER_NAME'] doesn't have a value when the PHP statements are run in CLI (Command Line Interface). Therefore the 'switch' statement can't do the necessary file 'include' and it takes the default case and tries to run the code in config.production.php. But that file just contains an opening (but no closing) PHP statement.


    My Solutions


    I've done two different solutions for this (on the 2 sites where I've encountered it). On one I removed the switch code and just pasted all the Perch statements into config.php as I knew I would not need a secondary set of sitepath and database setings. On the other site I've replicated the contents of config.www-mysite-com.php into config.production.php.


    Has anyone else come across this and what's your solution?

  • But that file just contains an opening (but no closing) PHP statement.

    While I expect you get a warning because $_SERVER['SERVER_NAME'] is not set in this context, a closing PHP tag is optional at the end of a file.



    Anyway, there is more than one way to handle this. You can rely on Perch production modes instead of $_SERVER['SERVER_NAME']:



    And config/env.php can just define the production mode. The value should be set according to the environment, so locally you'd have this:


    PHP
    1. define('PERCH_PRODUCTION_MODE', PERCH_DEVELOPMENT);


    And on your production server you'd have this:

    PHP
    1. define('PERCH_PRODUCTION_MODE', PERCH_PRODUCTION);



    Alternatively, you can look into environment variables.