need help with a cron job
Moderator: General Moderators
need help with a cron job
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!
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!
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Seems simple enough. Have a look at the PHP File System functions file(), fopen(), fwrite() and fclose().
in crontab add the command/line (use 'crontab -e' to open crontab editor):
That will backup your index.php every hour, to file newfile.php.bak
Code: Select all
0 * * * * cat /path/to/index.php > /path/to/newfile.php.bak 2> /dev/nullPut this code at the top of your crontab:
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.
Code: Select all
# min hour date month dayofweek (user) commandThen you just need to add this to make it run every hour:
Code: Select all
00 * * * * cp file1 file2I 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)
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
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.
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";
}
?>Thanks to everyone, this did the trick that I was looking for and it was easy.