skip to Main Content

I need to automate a couple things for my website, one of them being having the database cleared at midnight every night. I looked at cron job tutorials and came up with a script

<?php 
$con = mysqli_connect("localhost", "root", "password", "test");
$sql = "DELETE FROM markers";
$con->exec($sql);
echo "Record deleted successfully";
$con = null;

INSERT INTO `markers` (`name`, `lat`, `lng`, `id`, `questTitle`, `questReward`, `category`, `date_submitted`) VALUES ('Marker1', 41.2952, -92.644, 1, NULL, NULL, NULL, NULL);

this just wipes the database and re-inserts records with empty values to be modified throughout the day.

I tried setting up a cron job to run this at midnight using cpanel, my path is

/usr/bin/php /home/user/public_html/command.php

I have the command.php in my uploaded web files and set the job to run every 30 mins to test it, but no matter what I do I cannot seem to get it to work.

7

Answers


  1. What i noticed is that you are using

    /usr/bin/php /home/user/public_html/command.php

    Have you tried just using the plain php command only?

    30 * * * * php /path/to/php/file >> /path/to/desired/log/file/location

    My assumption is that the php is not installed on usr/bin directory.

    Login or Signup to reply.
  2. If you want to run a PHP script located in the public_html directory, the correct cron command will be:

    /usr/local/bin/php /home/**cPaneluser**/public_html/yourscript.php
    

    Or you can just use curl request as the cron job command.

    curl "https://www.yoursite.com/yoursctipt.php"
    
    Login or Signup to reply.
  3. You can set cron in linux based server like that

    0 0 * * * wget -O /dev/null your_url (http://localhost/test.php)

    it will run at every mid night.

    for set cron time please visit Crontab

    Login or Signup to reply.
  4. Try any of these:

    wget -O -q http://example.com/command.php

    php /home/user/public_html/command.php

    /usr/bin/curl http://www.example.com/command.php

    Also, make sure you enter your email address in the Cpanel cron job so you can at least know that the script is being ran on schedule, if it is then the problem is probably somewhere else.

    Also, in your code, there’s this line:

    INSERT INTO `markers` (`name`, `lat`, `lng`, `id`, `questTitle`, `questReward`, `category`, `date_submitted`) VALUES ('Marker1', 41.2952, -92.644, 1, NULL, NULL, NULL, NULL);`
    

    Putting this line literally in your script does nothing and should cause an error. You should execute this properly. E.g., Your script should look something like this instead:

    <?php
    $con = mysqli_connect("localhost", "root", "password", "test");
    $sql = "DELETE FROM markers";
    $con->exec($sql);
    echo "Record deleted successfully";
    $sql = "INSERT INTO `markers` (`name`, `lat`, `lng`, `id`, `questTitle`, `questReward`, `category`, `date_submitted`) VALUES ('Marker1', 41.2952, -92.644, 1, NULL, NULL, NULL, NULL);"
    $con->exec($sql);
    $con = null;
    
    Login or Signup to reply.
  5. You can simply use Your Hostinger Panel for doing the job.

    https://www.hostinger.com/tutorials/cpanel/#How-to-Create-a-Cron-Job

    Please check it out and you can help yourself from There.

    Login or Signup to reply.
  6. Question of bounty poster:

    I too have a php script that creates/modifies folders and files. It works perfectly when its address is loaded through a browser. The output from the cron job is accurate, but it’s not making any of the directory changes it should be. I’m also using Hostinger like OP.

    My take on it probably you are getting permission issue on cron it is working for server user but not for cron try with below command to work with your apache-user I am assuming apache server is what you are using.

    crontab -eu apache-user
    

    Or you can give the permission to required user or group to let them maniuplate directories and files something like below.

    chmod -R 655 /directory
    
    Login or Signup to reply.
  7. To run your script using a cron job the same way you do in the browser:

    /usr/bin/curl -o command.log '/home/user/public_html/command.php' >/dev/null 2>&1
    

    the -o redirects stdout, and “>/dev/null 2>&1” redirects stderr to stdout, which will let you see both the results and any errors in the command.log file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search