Page 1 of 1
need help with a cron job
Posted: Sun Aug 13, 2006 11:37 pm
by illmapu
Hi,
As an example, I would like to be able to create a backup of my index.php file, named something completely different, and then set a cron job to copy it to index.php every hour or so.
I feel stupid, but I do not know how to copy the file and have it write as another file and I'm not sure where I can find the answer.I have several cron jobs that set up already, but this one I do not have the knowledge in how to accomplish.
Can someone please help or send me to a post or tutorial where I can learn how to do this?
Thank you in advance!
Posted: Mon Aug 14, 2006 12:07 am
by RobertGonzalez
You logic evades me a bit. You say you want to make a backup of index.php and name it something totally different, then an hour later take what is in the differently named file and copy it back to index.php? And you want to do this every hour?
Posted: Mon Aug 14, 2006 12:50 am
by illmapu
Hi Everah,
Thank you for replying. I'll try to explain again using a hypothetical.
Let's say I already have a file named file.php. What I would like to do is use a copy of file.php and write it as file xyz.php with a cron job, if this makes sense?
I need to take the existing page and have it write as another page if it is possible? So , the cron would tell it to take page file.php and write it as xyz.php. I was thinking if the cron was in essence the file.php, it just needs to write as xyz.php at whatever time I need it to, but I don't know if it can even be done.
I'm not real good at explaining things at times, I hope this is a better way of explaining it.
Posted: Mon Aug 14, 2006 12:59 am
by RobertGonzalez
Seems simple enough. Have a look at
the PHP File System functions
file(),
fopen(),
fwrite() and
fclose().
Posted: Mon Aug 14, 2006 7:40 am
by illmapu
Hi Everah,
Thank you very much for helping, I will look at each of these (study...lol) and am hopeful that I will be able to get it done.
Thank you again!
Posted: Mon Aug 14, 2006 7:53 am
by Jenk
in crontab add the command/line (use 'crontab -e' to open crontab editor):
Code: Select all
0 * * * * cat /path/to/index.php > /path/to/newfile.php.bak 2> /dev/null
That will backup your index.php every hour, to file newfile.php.bak
Posted: Mon Aug 14, 2006 7:53 am
by Bigun
Put this code at the top of your crontab:
Code: Select all
# min hour date month dayofweek (user) command
This helps me when I create cron-jobs
Then you just need to add this to make it run every hour:
The zeros tell the computer that when the minutes hit zero (every hour) to run that command.
Posted: Mon Aug 14, 2006 8:09 am
by illmapu
Hi,
Thanks for replying. It's not that I want to make a backup of the file, I want to take the backup that I already have and write as another file name.
Jenk--- For example, make newfile.php and have it write as index.php, I think reverse of what you showed me.
Thanks again for any help!
Posted: Mon Aug 14, 2006 8:11 am
by timvw
I prefer the following layout for my crontab
#@reboot /usr/bin/fetchmail -d 1800 -f /home/users/timvw/.fetchmailrc
0 0 * * * /home/users/timvw/bin/chmoder.sh > /dev/null 2>&1
#1,31 * * * * /usr/bin/wget -O /dev/null
http://timvw.madoka.be/cron/feeds.php > /dev/null 2>&1
# * * * * * *
# | | | | | |
# | | | | | +--------- command to be executed
# | | | | +---------------- day of week (1 - 7) (monday = 1)
# | | | +------------------- month (1 - 12)
# | | +---------------------- day of month (1 - 31)
# | +------------------------- hour (0 - 23)
# +--------------------------------------- min (0 - 59)
Posted: Mon Aug 14, 2006 8:56 am
by illmapu
Hi,
Everah's helped me to complete this. (helps to study...lol)
Using the copy function, I created a fictitious page named test.php. I then used this code
Code: Select all
<?php
$file = 'example.php';
$newfile = 'example2.php';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
I wrote some text on the page example.php, changed the permissions of example2.php and when I go to test.php, it copied example to example2.
Thanks to everyone, this did the trick that I was looking for and it was easy.