Search code examples
phpcroncron-task

Schedule a simple php file on Cronjob on Godaddy


I am trying to run a PHP file every 15mins from 6PM. I have set cronjob like this:

*/15 18 * * * /usr/local/bin/php -q /home/ddruva/public_html/site_data/script.php

can someone confirm if this script will run at 6 PM and after that every 15mins?

The server is in MST standard time. So I have set 6 PM. I want the script.php to run from 6:00 AM IST to 7:00 PM IST. So I have set 6 PM MST.

In Godaddy, the process is not happening. Is there a way to check the log if cronjob has run successfully?

Thanks!


Solution

  • The server is in MST standard time... I want the script.php to run from 6:00 AM IST to 7:00 PM IST.

    Ok, so...

    • 06:00 IST is 17:30 MST (previous day)
    • 19:00 IST is 06:30 MST (same day)

    This presents two problems

    1. It crosses the midnight boundary, and
    2. The 30 min part of the offset makes it very difficult to create a repeating 15 minute pattern that starts / stops at the right time.

    The easy option is to ask your hosting provider to configure your server's timezone or try and use the CRON_TZ environment variable if available (probably not with Godaddy).

    CRON_TZ=Asia/Kolkata
    
    */15 6-18 * * * /usr/local/bin/php -q /home/ddruva/public_html/site_data/script.php
    

    Otherwise, you're left with something (ugly) like this

    # 5.30pm and 5.45pm
    30,45 17 * * * /usr/local/bin/php -q /home/ddruva/public_html/site_data/script.php
    
    # Every 15 minutes from midnight till 6am and from 6pm till midnight
    */15 0-5,18-23 * * * /usr/local/bin/php -q /home/ddruva/public_html/site_data/script.php
    
    # 6am and 6.15am
    0,15 6 * * * /usr/local/bin/php -q /home/ddruva/public_html/site_data/script.php
    

    With this approach, you're probably going to run into issues when your server changes between standard and daylight savings time and to be honest, I don't really have an answer for that.