0

I am new to using cron jobs. I am trying to run the script below on a daily basis. Go Daddy is my hosting service provider. I tried using Go Daddy's built in Cron Job Manager to create a cron job, with no success. I was wondering if someone could help me get started on creating a command that would run the following php script on a daily basis.

Is there a way to set this up by modifying the .php file alone?

I appreciate any advice.

<?php 
            #!/usr/bin/php

            //Create mysql connect variable

            $conn = mysql_connect('host', 'user', 'pass');


            //kill connection if error occurs

            if(!$conn){

                die('Error: Unable to connect. <br/>' . mysql_error());

            }

            //connect to mysql database

            mysql_select_db("mdb", $conn);



            $results = mysql_query("SELECT * FROM files");



            $name_array = Array();



            while($row = mysql_fetch_array($results)){

                $name_array[] = $row['name'];

            }

            shuffle($name_array);

            for($i=0; $i < 2; $i++){

                echo $name_array[$i];

            }



        ?>
Don
  • 863
  • 1
  • 8
  • 22
AnchovyLegend
  • 12,139
  • 38
  • 147
  • 231
  • 1
    You don't need to do anything in your code you just have to set it up to be run in the cron job manager `php /home/user/php_file_to_run.php` – Patrick Aug 02 '12 at 14:30
  • Also, stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) or [mysqli](http://php.net/manual/en/book.mysqli.php). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 02 '12 at 14:33
  • I saved the contents of the scrip above to cron.php, uploaded into command filepath as the 'command' on GoDaddy's cron job manager, however, the 'echoed' data seems to change every time I refresh, even though I set it to run hourly...? – AnchovyLegend Aug 02 '12 at 14:47

1 Answers1

1

There's no way to set it up in the file, it must be done with cron. If you have the ability to edit /etc/crontab, add this line:

01 0 * * * root /usr/bin/php -f /path/to/page

Otherwise, you'll have to follow the instructions here, or contact GoDaddy about why it's not working.

aynber
  • 22,380
  • 8
  • 50
  • 63