How to set up a PHP Cron Job in Host Gator

“Cron Jobs” are a way to run a script automatically, on a certain schedule.  For example, maybe you want to email all new users the day AFTER they sign up with some information.  Or maybe you want to charge customers on a particular schedule.

You could make your PHP script and just visit it manually every day, but there’s a better way: setting up a cron job.  Here’s Host Gator’s article, but it’s a little out-dated and doesn’t really have any examples.  This article gives examples, but they’re not clear enough (hence this post)

In the Host Gator cPanel, go to Advanced >> Cron Jobs.  Select how often you want the job to run from “Common Settings” and make your custom changes accordingly.

Here’s the part where the Host Gator documentation is lacking

In that second link, they give a lot of examples.  Here’s the command that worked for me:

php -q /home3/myUsername/public_html/mysite.com/home/cronjob.php

Notes/Tips:

  • Start off with php -q … don’t go with their other options
  • While they do bold the “username” (indicating it has to be changed) they do NOT bold  the number in the “home” part of the directory
  • Use the PHP function print getcwd() to find out the exact path (ex. the “/home4/…”)
  • Do NOT have any security checks for user authentication that redirect the user to a new page if they’re not logged in.  This isn’t a user running the script, it’s the computer.  As such, don’t even include the session_start() at the top of your script!
  • Start by testing your script “every minute” to make sure it works.  Then adjust the schedule to the appropriate time.

By the way: if you thought this blog post was helpful, check out the book I wrote on PHP, geared toward intermediate web developers.

  • 04.21.2017
  • PHP

No
Comments

Comments are now closed.