 |
Title: Using crontab Marked Cool (Review this resource) Author: econner Posted On: 2004-11-19 Category: Home > PHP Tutorials
Popularity:
Description: How to set up cronjobs with crontab and backing up an sql database with them.
Total Hits: 2841 Total Votes: 1
Total Points: 2 (1 reviews)
Page Navigation: [1]
|
Using
crontab to execute php scripts
|
|
Website automation
can be really helpful to your website. Whether its backing up (which you
definitely will need) or running a script to update an sql database, you'll
need crontab. In this tutorial I will show you what crontab is, how to
use it and an example to backup a database with it.
Crontab is
a *nix program that is used run commands at specific times. Ok, lets begin:
Type crontab -e
You should get something similar to this:
Ok now type
'i' and input something similar to: 30 * * * * /home/virtual/site/fst/var/www/html/cron.php.
You should get something like this:
Now
push Escape then type :wq (it tells it to write the changes and then quit)
It should say 'installing new crontab'
Now to view your crontabs you can type crontab -l
Ok so what did we just do?
We set up a cronjob that will run the script cron.php every 30 minutes
and output nothing. Here is a summary of the fields:
(note the *
means all)
Now for one that will run once a week at 1 AM
0 <- tell it to run on 0 minutes
1 <- run at the first hout
7,14,21,28 <- run at intervals of 7 seven days
* <- all months
* <- all days of week
/home/virtual/sitee/fst/var/www/html/cron.php <- command
> /dev/null <- what to output (nothing in this case)
Ok now lets create an example cron job, it will be a script that runs once
a day and will backup our database.
We'll use
php:
#!/usr/bin/php -q
<?php $user = "username"; $pass = "password"; $db = "dname"; $path = "/"; // where to back up files
$file = $path."backup-".Date("m-d");
@exec("mysqldump
-u '".$user."'
-p".$pass."
--opt ".$db."
> ".$file.".sql");
?>
Now set up
a cron job that looks like this:
0 1 * * * php -q /path/to/phpfile/backup.php > /dev/null
Thats all, This should run this backup script once a day at 1 AM. Have
fun :).
|
|
Page Navigation: [1]
|
|
 |