You can use cPanel's Cron Job feature to run Unix commands and also automate the execution of your PHP, Perl, or Python Scripts. To create a Cron Job, first log into your cPanel and select "Cron jobs:"

cron1

Add a New Cron Job

Cron jobs use the following time field sequence:

[minute] [hour] [day] [month] [weekday] [command]

48 09 * * * /my/script -v

This will run every morning at 9:48 AM. The * is a wild card, meaning "every day/month, etc" For example; 48 09 * * * = every 48 minutes of every 9th hour of every day of of the month.  Just to clarify, the setting for "Weekday" should be interpreted as day of the week, including Saturday and Sunday. Many people think of weekday as just Monday through Friday, but when referring to cron job settings, it actually means every day of the week.



IMPORTANT:

It is suggested that Cron Jobs not be scheduled at the top or bottom of the hour. Remember that you are not the only user on the server who may be using a cron job. If all the cron jobs were scheduled at the same time, the server performance would be slowed when it tries to execute all of the scripts. So, for example, the best times to set a cron job to run may be at an odd minute such as the example above. It is set to run at 9:48 every morning.  Help keep server performance at an optimum level by setting the cron job execution at an odd time.





Specify the command to run the php script

The command should have two parts:

[program to run (perl/pyth./php)] [switches] [script]

The program to run will specify the type of script you are executing, and will either be:

  • php 
  • python
  • perl

The script will need to be connected directly to its location on our server, so it will be something similar to: /home/username/public_html/path/to/script, with "username" being your username.

  • To run a PHP script: php -q /home/username/public_html/scripts/testrun.php
  • For Python scripts: python /home/username/public_html/path/to/script
  • For Perl/CGI scripts: /home/username/public_html/cgi-bin/yourscript.pl

Alternative to wget command

Most scripts that are run through wget can be ran through lynx or curl commands, which are allowed in Cron Jobs. Be sure to set the user agent in any lynx or curl commands you run. If the user agent isn't set, mod_security rules on the server will prevent the commands from running. For example:

curl -A -s useragent (URL)

curl -A "Mozilla 4.0" http://mydomain.com/myscript.php

lynx -dump -useragent (URL)

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Did you find this article helpful?

Comments

2013-07-15 5:57 am
I have a script to run that has arguments that need to be passed to it, is there a solution for that?
Staff
12,339 Points
2013-07-15 5:17 pm
Hello blaincar,

I'm not sure exactly what you are trying to accomplish, but I found a forum post via Google search on How to Pass a variable to a php cron job. I hope this helps get you going in the right direction.

If you have any further questions, feel free to post them below.
Thank you,

-John-Paul
Staff
2,342 Points
2013-09-16 11:38 pm
Hello blaincar,

Yes, it can. You need to put double quotes around the URL parameter of the curl command, otherwise the operating system tries to interrupt the HTML parameter as if it is a cURL parameter.

So the example in the article above should be:
curl -A "Mozilla 4.0" â??http://mydomain.com/myscript.php&myparameterâ??

Working example to run the WordPress â??WP-Cron Controlâ?? plugin:
curl -A "Chrome 30.0" --silent "http://www.domain.com/wp-cron.php?doing_wp_cron&62647ba73f6dd22c3be278c1cc5c027a" > /dev/null 2>&1
n/a Points
2014-04-30 10:02 pm

I'm trying to make this cron job run, can you help me?  I was told I need a user agent, not sure what that is or how/where to add.  I'd appreciate any help you can provide.  Thanks!  

lynx "http://www.example.com/index.php?option=com_rsmail&task=autoresponders"

Staff
3,713 Points
2014-04-30 10:50 pm
Hello Paul, and thanks for the comment.

If you need to specify a user-agent for a cronjob that is requesting a specific script like your example, you would want to typically use curl instead of lynx:

curl --user-agent CronJob "http://example.com/index.php"


If you did want to still use lynx you should be able to use:

lynx -dump -useragent=CronJob "http://example.com/index.php"


Please let us know if you're still having any issues at all.

- Jacob

n/a Points
2014-04-30 11:07 pm

Thanks Jacob, I did try curl, but I don't know what my "useragent" is, where do I get that or what do I put there to make this work?  Plus you have "CronJob" in your string, I'm not sure what I would replace that with either.  Unfortunately I'm not a developer, but my developers are shrugging their shoulders and telling me to figure this out, so any help for a layman would be greatly apprecaited.  Thanks!

Paul

Staff
3,713 Points
2014-04-30 11:33 pm
Hello again Paul,

You literally can use anything you want for the User-Agent string. There is just a very common ModSecurity rule on most servers that will block any requests that are coming from a blank User-Agent. If you're using a web-browser, or any normal kind of HTTP client they are going to send their own string. For instance I'm using Google Chrome right now, and here's my User-Agent string:

"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36"

However things like lynx, curl, and wget don't include User-Agent strings by default.

I typically like to use something descriptive like CronJob, so that when I'm looking at my website's access logs I can be reminded those requests were coming from a cronjob I setup and not from a unique user.

- Jacob
n/a Points
2014-05-01 4:51 am

Thanks Jacob, it appears it's working now.  I really apprecaite your help with this.

 

n/a Points
2014-07-03 9:46 am

I am trying to add cronjob with flat url

domain.com/controller/action

php -q http://domain.com/controller/action >/dev/null 2>&1

This is not working.

Code is proper and its working another server.

Staff
3,713 Points
2014-07-03 10:47 am
Hello shravan,

That syntax looks correct, I'm assuming going to that URL in your web-browser it is working? Also obviously it needs to be a PHP script that is run when that URL is called for the PHP command to work on it.

You might want to try to remove the /dev/null 2>&1 part of the code so that it does email you the output of the cron job, hopefully showing any errors that might have been encountered.

Sometimes, you'll also have better luck by directly calling the PHP script within a cron job such as:

cd ~/public_html/controller; php -q action.php

Please let us know if either of those helps.

- Jacob
n/a Points
2014-08-18 3:52 pm

I have an issure with my cron, they all work awesome, no flaws.. the issue is my root of my ftp is filled with (filename).php1 .php2 php3 etc etc.. Ive been going into for the past year monthly and deleting them.. but there gotta be a way to prevent these from being created ... right?

example of cron.. 16     10,10     *     *     *     /usr/bin/wget http://www.mysite.com/blah/firstrun.php>/dev/null 2>&1 am I looking for cURL instead of wget? or -q or -a   Can youcopy paste the above and  rewrite it for me from above so its correct.   Thanks!

Staff
16,266 Points
2014-08-18 7:13 pm
Hello Great Page,

Are you just getting more copies of the same file you are saving in your account? If so, simply add code to delete the current copy if it exists.

Also, as per the article, curl is an option to wget. The format you want to use is:
curl -A "Mozilla 4.0" http://mydomain.com/myscript.php


Kindest Regards,
Scott M
n/a Points
2018-01-25 8:14 am

Puttin this here in case someone else found it usefull:

I had some issues with a php script that required extra params, the crontab executed the php script but the params werent passed, example sentence for the crontab:

php index.php param1 param2 param3

Running the command directly from terminal executed as expected, but running it from crontab result in only executing the script "index.php" without the parameteres included. Finally, what get the script running correctly with parameters was using "php-cli" instead of "php":

php-cli index.php param1 param2 param3

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

Related Questions

Here are a few questions related to this article that our customers have asked:
Would you like to ask a question about this page? If so, click the button below!
Need More Help?

Help Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.
}