executing PHP file via cron in cPanel

 David

UPDATED on Jan 9th, 2021
I found much easier way to schedule a cron job while back but it looks like I forgot to share that here. If you are using MVC framework, executing PHP file may not be straightforward and it will be much easier to execute a route instead and there's easy way to do that in cPanel.

curl "https://handymango.com/my-cron-job" >/dev/null 2>&1
The command above will call that route url for specified interval and the best part is that you can actually call external url as well!
I actually have a cron job scheduled in cPanel from another hosting server and the cron executes a url in different hosting server that does not have cron job.
I love cPanel!

ORIGINAL POST from November 1st, 2010
Cron refers to a time-based job scheduler in Unix-like computer operating systems.

A cron is useful for scheduling certain types of task on the website so that they can be done on a regular basis without having to manually do it or reminded to do it.

Here are some examples on how crons can be used on an website:
1. Every 1st day of the month, delete all temporary files on the web server that are not being used.

2. Archive or back up files and/or database once a week.

3. Email the website admin with the website's statistics at the end of the day.

As you can see from these examples, you can easily see the value of a cron. It basically automates the work that you normally would have to do manually if you didn't have the cron capability.

My website has a cron that gets executed every 5 minutes. You might be wondering, what is so important that I need to do it every 5 minutes on my website?

Well, it's no other than my custom built tweets.
I basically send an email with a picture(s) attachment to a specific email address and my cron checks that email's INBOX every 5 minutes to see if there are any new emails. If a new email is found then the cron will retrieve the email and post it to the tweet section of my website with the picture attachment. This is great since I can send emails with pictures from my iPhone virtually from anywhere!

Although there's potentially 5 minute delays in posting, it's so much faster and easier than trying to post something from a browser on the iPhone.

Therefore, I wrote a PHP script that checks a specific email INBOX then retrieves the emails, saves the attachment files, then insert them into a MySQL database table, which is equivalent to posting a blog on my website.

Once I had my PHP script written and manually tested it, I had to find a way to schedule it. Fortunately, I have cPanel as the control panel of my hosting and cPanel includes Cron jobs.

From the Cron jobs page in the cPanel, all you have to do is specify how often you want it to be executed then type "php [full path to the php file to be executed]" in the Command text box.
For example,



Once you click on the "Add New Cron Job" button, this is how it would look like:


The scheduling option is very flexible. For example, the sample schedule cron in the screenshot above indicates that cron should execute the mycron.php file located on /home/mywebiste/www/ directory at 9:45am on 1st and 15th day of every other month.

By default, the output of each cron execution is sent to the email address identified on the Cron Jobs page. I would recommend that you schedule the cron to run every minute first so that you can receive the cron output email immediately (every minute). If your php script has an error, then the email will include the error message. Your scheduled php script should have "Successful!" or "Completed without error" echo output at the end so that they would be included in the email, which is a clear indicator that the cron executed the script without any issue.
Obviously, you can have it output any other useful information, such as the number of records or files processed, etc.

Once you have confirmed the successful email from cron, you can then schedule it to be executed on your desired frequency.

If you do not wish to get email confirmation from cron, you can append the >/dev/null 2>&1 at the end of the command, which I did since I do not want to receive email from my tweet cron every 5 minutes

Please note that the most common mistake made when setting up the cron is specifying the incorrect path to the php file. If you are not sure what the full path to the file is then include the following line in any php file and it will show you the full path:
echo $_SERVER["SCRIPT_FILENAME"];

One last thing to remember is that you must set the file permission to be executable on the PHP file. I usually set it to 755.
Go Back to List Page

This post has 1 comments

Name : Comment : view emoticons
Please consider signing up for our website.
If you sign up and log in:
  •   You can avoid the "I'm not a robot" captcha when commenting
  •   You can also avoid typing your name every time
  •   You can upload a picture for each comment
  •   You can change or delete your comment within 1 hour
  •   You can track all the comments you posted on this site
  •   You can read blog posts that are only open to members
  •   You can look up blogs using the search feature
  •   More privileges for our friends & families coming...

OK, Sign me up!

Emoticons are a great way to visually express how you feel.
However, there are times when unintended content is converted to emoticon because the content happens to have one of the emoticon symbols. That's why it's always good idea to preview your comment before posting and when you see this type of problem, you can indicate NOT to auto convert.