Running cron on container startup prevents PHP script from showing webpage

Viewed 116

I'm attempting to construct a monitoring webservice using docker & PHP

I have a PHP script that when accessed performs a check to see if a number of services are running (through checking the HTTP headers for the services enpoints).

I have a functions.inc.php file and an index.php file

Here is my index.php file

<?php
header("Access-Control-Allow-Origin: *");
header("Content-type: application/json");
require('functions.inc.php');

//connect to MySQL DB

include("conn.php");


//Log file - check if it exists - create one if not
$file = "/var/www/html/logs/log".date("Y-m-d").".txt";
if (!file_exists($file)){
    echo "Log file for today does not exist... creating and writing logfile\n";
    $logdata = "Monitoring log for: ".date("Y/m/d"."\n");
    file_put_contents($file,$logdata,FILE_APPEND);
}else{
    echo "Log file for today exists\n";
}

//service.csv can be configured to add 
//or remove services to the monitoring

//Open service config csv
//Check if service.CSV is openable

$services = fopen("/var/www/html/service.CSV",'r');

if($services){
    //Read first line
    fgetcsv($services,1000,',');
    //loop through each service - perform check and write result to file
    while(($value = fgetcsv($services,1000,','))!== FALSE){
        $service=$value[0];

        if(servicecheck($service)){
            $response = "[".$service." - is running]\n";
        }else{
            $response = "[".$service." - is not running]\n";
        }

        echo $response;
        file_put_contents($file,date("[h:i:sa ")."]".$response,FILE_APPEND);
    }
}else{
    die("Unable to open file");
}

I can currently run the script through buidling and running it through docker, making it accessible through localhost port 80.

Here is my initial Dockerfile:

FROM php:7.2-apache
COPY src/ /var/www/html/

RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

RUN chmod 777 /var/www/html

This all works as intended. I can access the service through localhost:80 on my browser.

However, I want these service checks to run even when the page isn't accessed.

I found that cron would be the appropriate solution for this, and I tried to implement it. My new dockerfile looks like this-

FROM php:7.2-apache
COPY src/ /var/www/html/

RUN docker-php-ext-install mysqli && docker-php-ext-enable mysqli

RUN chmod 777 /var/www/html

RUN apt-get update && apt-get -y install cron
# Copy hello-cron file to the cron.d directory
COPY hello-cron /etc/cron.d/hello-cron
 
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Apply cron job
RUN crontab /etc/cron.d/hello-cron

RUN chmod 777 /etc/cron.d/hello-cron

RUN touch /var/log/cron.log
CMD cron && tail -f /var/log/cron.log

This does work. In the docker terminal I can see the script running every minute. It outputs to terminal what until now has been output to the browser. However now I can't access it through localhost:80 and make the php script run manually.

heres my cronfile (hello-cron) for reference

* * * * * /usr/local/bin/php /var/www/html/index.php >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.

I believe the issue lies with cron, as its the final line CMD cron && tail -f /var/log/cron.log that makes the page inaccessible.

Is there any solution for this? I'm relatively new to it all so I'm sure there is something I'm missing but I cant find anything that helps

0 Answers
Related