Home arrow Site Administration arrow Cron Job Tutorial: Crontab Scheduling Syntax and Script Example

Cron Job Tutorial: Crontab Scheduling Syntax and Script Example

This is a beginner tutorial on using crontabs or cron jobs in your LAMP (Linux/Apache/MySQL and PHP) web hosting account. Crontabs can be used to automatically execute server side commands, such as a PHP script, at periodic intervals. You can specify these periodic intervals, whether you want the script to be executed hourly, daily, weekly, and so on.

TABLE OF CONTENTS:
  1. Cron Job Tutorial: Crontab Scheduling Syntax and Script Example
  2. Configuring Crontab to Automatically Run the Script
By: Codex-M
Rating: starstarstarstarstar / 3
December 07, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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

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.



 
 
>>> More Site Administration Articles          >>> More By Codex-M
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

SITE ADMINISTRATION ARTICLES

- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: