Home > database >  Cronjob to check if service is running, if not starting service
Cronjob to check if service is running, if not starting service

Time:09-25

I need a cronjob, that check if a page is running. I think by check if the HTTP response status code is "200".

If the status code is not "200" a bash code must be executed.

I hope someone can help me with this topic. I tried it by myself but I have no idea how to get the code working.

CodePudding user response:

You can use it like..

#!/bin/bash

status_code=$(curl --write-out %{http_code} --silent --output /dev/null https://www.my-site.com/)

if [[ "$status_code" -ne 200 ]] ; then
    ## do your stuff here
  echo "Site status changed to $status_code"
else
  exit 0
fi

CodePudding user response:

I have setup this code to check if the httpCode is 200. This part of the code is working fine. But the shell_exec (else) is not working. Is there something wrong with? Maybe someone have an example for some cronjob in php where a bash command get executed.

<?php
    $url = "https://xxx";

  $handle = curl_init($url);
  curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
  
  /* Get the HTML or whatever is linked in $url. */
  $response = curl_exec($handle);
  
  /* Check for 404 (file not found). */
  $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
  
  if($httpCode == 200) {
      /* Handle 200 here. */
      $output = "Status 200 OK";
  }
  else{
      $output = shell_exec('cd public_html/redmine/ && bundle exec ruby bin/rails server -b webrick -e production -d');
      
  }
  
  curl_close($handle);
  
  /* Handle $response here. */
echo($output);


  ?>

  • Related