need help with a cron job

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
illmapu
Forum Commoner
Posts: 47
Joined: Fri Aug 22, 2003 1:48 pm

need help with a cron job

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
illmapu
Forum Commoner
Posts: 47
Joined: Fri Aug 22, 2003 1:48 pm

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Seems simple enough. Have a look at the PHP File System functions file(), fopen(), fwrite() and fclose().
illmapu
Forum Commoner
Posts: 47
Joined: Fri Aug 22, 2003 1:48 pm

Post 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!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Post 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:

Code: Select all

00 * * * * cp file1 file2
The zeros tell the computer that when the minutes hit zero (every hour) to run that command.
illmapu
Forum Commoner
Posts: 47
Joined: Fri Aug 22, 2003 1:48 pm

Post 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!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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)
illmapu
Forum Commoner
Posts: 47
Joined: Fri Aug 22, 2003 1:48 pm

Post 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.

Post Reply