Accueil > Système > Cron Job Tutorial: Crontab Scheduling Syntax and Script Example

Cron Job Tutorial: Crontab Scheduling Syntax and Script Example

29/01/2024 Categories: Système Tags: , , ,
Print Friendly, PDF & Email

source: Devshed

Crontabs is very useful when doing website maintenance. You can use this feature for the following purposes:

  1. Deleting all files in a certain folder at regular intervals. If you accept user-uploaded files, then eventually it will clog your web hosting server. If you do not regularly delete these files, they will consume a lot of disk space and can slow down your website.
  2. Deleting files of a specific type at regular intervals. You can also choose to delete files of a specific type, instead of deleting all of the files in the folder. If you have PHP, HTML and MP3 files together inside the folder, you can delete only the MP3 files.
  3. Regularly backing up your MySQL database. This is one of the most important webmaster tasks. By using crontabs or the cron job feature, you can create a PHP script that will back up a selected MySQL database, and have it execute automatically executed at a specific, regular interval (e.g monthly, yearly, etc).

This tutorial will focus on creating a cron PHP script application, then configuring your cron hosting feature to execute these scripts automatically.

Deleting All Files Within a Folder using Cron

Let’s have an actual example. Below is the PHP script that will delete all of the files in the folder.

<?php
/*
First  protect this file from direct access. If unauthorized users execute  this script using a browser, then they will receive "Direct File Access  Prohibited" Errors.
*/
if ('deleteallfiles.php' == basename($_SERVER['SCRIPT_FILENAME'])) {
die ('<h2>Direct File Access Prohibited</h2>');
}
else {
/*

If this is not a direct file access, execute the script. Next we define which folder to clean in your FTP server. Bear in mind that this is a full server path to the folder. This should start with a forward slash and end with a trailing slash. To determine the full server path, you need to create a PHP file with this script and save it as fullserverpath.php:

<?php
echo $_SERVER['SCRIPT_FILENAME'];
?>

Then upload it inside the folder that you need to clean. Execute the script in the browser, e.g http://www.yourdomain.com/folderthistoclean/fullserverpath.php You will see the full path to this folder, such as: /home/www/php-developer.org/testdelete/fullserverpath.php

Lire aussi:  How can I find out if a specific program is installed?

This means that the full server path to the folder should be: /home/www/php-developer.org/testdelete/, then use this one to define $foldertodelete:

*/
$foldertodelete  = '/home/www/php-developer.org/testdelete/';
//Delete entire files in the folder , this is using *.*, this means all files.
$fileTypes1 = '*.*';
/*
Define  how old the files in minutes should be when they are deleted. For  example, if you set the expiration time to 3 minutes; this means that  all files older than 3 minutes will be deleted by this script during  cron execution.
*/
$expire_time1= 3;
//Find all files  in the folder using a loop
foreach (glob($foldertodelete . $fileTypes1) as $Filename1) {
/*
Read file creation time  using filectime function. Read more about this function here: http://php.net/manual/en/function.filectime.php
*/
$FileCreationTime1 = filectime($Filename1);
/*
Calculate file age in seconds. The difference between the current time and file creation time.
*/
$FileAge1 = time() - $FileCreationTime1;
//Is the file older than 3 minutes?
if ($FileAge1 > ($expire_time1 * 60)){
//If yes, then delete file.
unlink($Filename1);
}
}
}
?>

This is the complete script: http://www.php-developer.org/wp-content/uploads/scripts/deleteallfiles.txt

If you would like to delete files of a specific type, instead of all of the files in the folder, you can use this script: http://www.php-developer.org/wp-content/uploads/scripts/deletespecificfiles.txt

Bear in mind that the two above scripts can only delete files, and not a folder.

Categories: Système Tags: , , ,
Les commentaires sont fermés.