Hi,

I've been searching the web for a actual explanation on what a cron job does, I get on how to set it but I'm not completely sure on how to make it run a script.

I might sound a bit dumb but I just can't get my head around it, so in example:

I've got my website, 100+ urls being pinged (currently on accessing the site so this freezes the website for aprox. 40 sec) I want this to happen in the background and make a cron run the script to ping every 2 minutes.

Does this mean I need to create a separate "script.php" with the curl ping script in it that used to be and link this somehow to the original page where the ping process was done before or not.

Or does it mean that I create a cron script that auto refreshes the page every 2 minutes?

As I said I'm getting a little confused because all the tutorials I find on the web just explain how to create the cron but not how to link a script to replace a already existing process.

Sorry for the silly question lol

    1. Write a script that, when run, pings the URLs.
    2. The script will save the result somewhere, e.g., in a database.
    3. Configure a cron job to run this script periodically.
    4. Write another script -- a webpage -- that will retrieve the most recently saved result, format it, and display it to users who access it.

    laserlight

    Currently without the cron I'm running it like this:

      <?php
    
    require_once "config/config.php";
    
    $sql = "SELECT * FROM deployments";
    	if($result = mysqli_query($link, $sql)){
    		if(mysqli_num_rows($result) > 0){
    			while($row = mysqli_fetch_array($result)){
    				?>
    					<tr>
    						<td> <?php echo $row['id'] ?></td>
    						<td> <?php echo $row['server'] ?></td>
    						<td> <?php echo $row['name'] ?></td>
    						<td> <?php echo $row['url']?> <a href="http://<?php echo $row['url']?>" target="_blank" style="color: grey;"><span class="fa fa-external-link"></span></a></td>
    						<td></td>
    				<?php
    					  $url = $row['url'];
    					  $ch = curl_init($url);
    					  curl_setopt($ch, CURLOPT_NOBODY, true);
    					  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    					  curl_exec($ch);
    					  $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    					  curl_close($ch);
    					  if (200==$retcode) {
    						    echo "<td><span class='badge badge-success'>LIVE</span></td>";
    					  } else {
    						    echo "<td><span class='badge badge-danger'>DOWN</span></td>";
    					    $path_to_file = './emailtemplate.html';
    					    $file_contents = file_get_contents($path_to_file);
    					    $file_contents = str_replace("depplaceholder","$url",$file_contents);
    				        $to = "---";
    				        $subject = "$url down";
    				        $headers = "From:Deployment Monitor <--->" . "\r\n";
    				        $headers .= 'MIME-Version: 1.0' . "\r\n";
    				        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    					$message = $file_contents;
    
    			        	mail($to,$subject,$message,$headers);
    				  }
    				 ?>
    
    					<td><a href="config/remove.php?id=<?php echo $row['id']; ?>" style="color: grey;"><span class="fa fa-trash"></span></a></td>
    				</tr>
    	<?php
    				}
    				mysqli_free_result($result);
    			} else{
    				echo "No records matching your query were found.";
    			}
    	} else{
    			echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
    	}
    
    	?>

    I'm still new tot php, getting the hang of it but I'm not sure on how to make the cron script run the curl ping, post the data to a row that applies to:

    	<tr>
    	  <td> <?php echo $row['id'] ?></td>
    	  <td> <?php echo $row['server'] ?></td>
    	  <td> <?php echo $row['name'] ?></td>
    	  <td> <?php echo $row['url']?> </td>
              >>>>>><td> <?php echo $row['status']?> </td><<<<< cron would push data to this table per url after pinging to either LIVE or DOWN and would post the data in this <td>
           </tr>

    Sorry for asking a lot lol I'm just confused and google isn't helping since there seems no answer to my specific question.

      It might help if you understood it as a PHP script (not a "cron script") that is not a webpage, but rather a standalone script. The script would use cURL and database functionality, but would not output any HTML. So, you would extract those parts of your current script that have to do with pinging and writing to the database. The parts that have to do with printing HTML output would go in the script I mentioned in point #4.

      EDIT:
      Oh, you're only accessing the database to retrieve the list of deployments. Nope, that's not enough. You need to update the database with the data from the ping, i.e., to record whether the URL is alive or down. If it is down, a more sophisticated approach would be to add the site details to a message queue, then you have a third PHP script process the queue and send the emails.

        CRON: https://www.freebsd.org/cgi/man.cgi?query=cron&sektion=8&apropos=0&manpath=FreeBSD+12.0-RELEASE+and+Ports

        CRONTAB: https://www.freebsd.org/cgi/man.cgi?query=crontab&sektion=5&apropos=0&manpath=FreeBSD+12.0-RELEASE+and+Ports

        You might be a windows user, in which case perhaps you've heard of "Scheduled Tasks"? Scheduled Tasks was probably modeled on Cron(8).

        Cron(8) runs continually on most Unix-like systems; it wakes up every minute and reads the user and root crontab files, and if there's anything in them scheduled to run at that minute, it attempts to run the script using its shell.

        So if you don't know much about operating in a shell environment, you've got some catching up to do.

        Here's an example crontab file:

        #FOO.com CRON SYSTEM
        
        # Minute        Hour    Day     Month   Weekday         Command
        */6             *       *       *       *               /www/www.foo.com/cron/frequent.php > /dev/null 2>&1
        59              *       *       *       *               /www/www.foo.com/cron/hourly.php > /dev/null 2>&1
        02              0       *       *       *               /www/www.foo.com/cron/daily.php
        00              0       *       *       *               /www/www.foo.com/tools/wlizer

        Every six minutes the system runs frequent.php. At 59 minutes after every hour it runs hourly.php. At midnight it runs wlizer and two minutes later runs daily.php.

        Now, these PHP files are meant to be run in the shell/CLI. Each starts with a shebang line:

        #!/usr/local/bin/php -q
        <?php
        
        do_stuff(); 

        Note that PHP isn't always at /usr/local/bin/php on every system/server, so some installations would have a different interpreter line there, while others might attempt to use env to have the system locate PHP itself for portability's sake:

        #!/usr/bin/env php
        <?php
        
        do_stuff();

        Note that as Laserlight points out, cron(8) doesn't have anything to do with the web. There's no need to generate HTML unless you intend to view the output from a job in a browser at a later time.

        However, cron(8) does by default send an email to the user who's crontab it being run, unless specifically instructed not to. In the example crontab above, I've redirected both stdout and stderr to /dev/null (a blackhole, basically) for frequent.php and hourly.php, but I WILL get an email for the daily.php job.

          2 months later

          I'm going to add to the above just a little. Cron(8) doesn't only run PHP files ... it can run any program that can be executed from within its environment, which could be just about anything on the system it can find and has permission to be. It could be Perl, Python, or Ruby scripts, C or C++ programs, shell scripts or even a shell built-in equivalent:

          #Minute  Hour    Day     Month   Weekday   Command
          1        1       *       *       *       /bin/cat /path/to/error_log > /home/me/yesterdays.errors
          30       7       *       *       *       /usr/local/bin/mysqltuner.pl | /bin/mail -s "MySQL Server Report" root@mydomain.com
            Write a Reply...