How to Schedule a Script to Execute With Cpanel

Scheduling a script to run with cpanel is fairly simple. The hardest part is getting the command syntax correct.

To schedule a script on Linux based hosting (as opposed to Windows based hosting), we use what’s called a ‘cron job’. According to Wikipedia, ‘cron’ comes from the Greek word for time – chronos.

Scheduling a cron job requires that you input the time you want it to execute (which includes the frequency) and which script you want to execute at that time. You can also include an email address where the output from the script can be sent. If there is no output from the script, you will at least get notified that the script has executed successfully or not assuming you set up the email address correctly.

Entering the time you want the script to execute can be a little cryptic, but cpanel has made that fairly easy as well. There is a drop-down box for popular selections at the top of the scheduling portion of the screen, such as every minute, once a day, once a week, etc. Choosing one of these options will set all the other text boxes correctly.

These other text boxes are for the minute, hour, day of the month, month of the year, and weekday. Cpanel also includes drop-downs with popular input for each text box. Asterisks in the text box usually means ‘every’. So, if I put an asterisk in the minute box, it’s going to run every minute. An asterisk slash number, such as */5 in the minute box, means every 5 minutes. Putting a number with no asterisk will run the script on that minute during the hour. In other words, if I put a 10 in the text box for minute, the script will run 10 minutes after the hour. Which hour will depend on how I set the remaining text boxes for the schedule.

The last text box is for the command. This is always the trickiest for me. You need to get the path to your script correct or, obviously, the script won’t execute because cron won’t be able to find it. The path is NOT the same as your URL for the script. This means that you can store the script in the private area of your website. This keeps others from being able to execute your scripts.

Your path, on most cpanel systems will be /home/username/scriptname.php if it’s in the private area of your website. The username is the username for your website. Of course, the scriptname.php is whatever you named your script. It doesn’t have to be a .php file, but that’s usually what I’m working with, so my examples will be for PHP scripts.

Also, just giving the path will not execute the script. It must be a command that you would use on the command line of a Linux computer. Most people are not familiar with the command line of any computer any more because most of us use graphical interfaces – icons, buttons, etc. – instead of doing things the old fashion way with just text.

For my php scripts, the command is php path/file.php so in the example above it would be:

php /home/username/scriptname.php

Then you need to save the cron job. Cpanel will list all your cron jobs for you.

Don’t forget to set your email address at the top of the screen if you haven’t before. This will let you know if you have things set correctly the next time the script runs.

If you don’t receive notification when you think you should, this usually means you have something set wrong. For me, it’s usually that I left the php off the command line. I often forget and just put the path. For some reason, I seem to think it should be smart enough to figure out that since it’s a php file, it should know to run php. But, it’s not and my script doesn’t run.

Other times I forget to set the email correctly and the script runs but I don’t get notified. And of course, there’s always room for error in the scheduling.

Once you get it set correctly, it’s a VERY handy tool!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top