-2

I have a database with a bunch of links that I want to keep updated. Basically if a link returns a 404 error code I want to remove it from the database. I have a script that I am using however I need to run it manually. How can I make this work using CRON?

Devin M
  • 9,283
  • 2
  • 27
  • 44
Sujit Tiwari
  • 253
  • 2
  • 5
  • 15
  • 1
    So, your question is how to run a php script in cron? –  Mar 16 '12 at 17:24
  • possible duplicate of [Use PHP to create, edit and delete crontab jobs?](http://stackoverflow.com/questions/4421020/use-php-to-create-edit-and-delete-crontab-jobs) Not sure if that's what you're looking for but it might get you on the right track. – Wesley Murch Mar 16 '12 at 17:37
  • Why not redirecting 404 error to a custom php page, and then delete the link from there? In that case no CRON script is needed, and links are immediately updated. – kappa Mar 16 '12 at 17:31
  • If the links are external to the OP's site, that won't work ... – keithhatfield Mar 16 '12 at 17:32
  • From my understanding these are 3rd party links in which case he cant redirect the error page. – Devin M Mar 16 '12 at 17:32
  • my links are external which is stored in database .i am calling them in iframe but with time few links becomes dead i want to delete that links . i know how to run cron but what to run in cron can anybody tell so that i can delete dead links from my database – Sujit Tiwari Mar 17 '12 at 16:57

3 Answers3

3

in your shell as cron user (or root):

crontab -e

This will bring up your crontab file in your editor. Add a new line something like this:

*  */12  *  *  *  /path/to/script

Save/exit the file.

Now for a quick lesson on cronjobs:

-The first 5 arguments in the line tell how often, or when the cron daemon will execute the 6th argument.

-From left-right, arguments represent: minutes, hours, days, weeks, months

-An asterix (*) tells the cron to run on all values of it's associated time measurement (example * * * * * means to run every minute, of every hour, of every week, and of every month!)

In my example, * */12 * * * means to run every 12 hours.

Check out: http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/

Ben Ashton
  • 1,335
  • 10
  • 15
1

To run a PHP script with cron you can use the PHP executable and the path to the script. On most linux systems you want to edit your cron file (the crontab) with the command crontab -e. This will open up a command line based editor and you can just append your new job to the bottom of the file using this format.

<minute> <hour> <day_of_month> <month> <day_of_week> php /path/to/script

If the commands dont work for you let me know what distribution you are using and I can modify the instructions.

Devin M
  • 9,283
  • 2
  • 27
  • 44
0
/usr/bin/php -q /home/user/public_html/script.php
Norse
  • 5,304
  • 13
  • 47
  • 84