How to Create a Cron Job in cPanel

Feb 01  07

It doesn’t matter what software you’re using to power your blog or Web site—WordPress, Textpattern, Drupal, Pligg, etc. At some point, there’s a good chance you’ll need to run cron jobs to automatically execute various tasks on your preset schedules.

For example, it’s a very good idea to schedule automatic backups of your database. Your site may also require the regular, automatic execution of a script to update data (for example, if you use the RSS Importer with Pligg, you’ll want to automatically update your feeds). I’ll show you how to do both.

There are two ways to set a cron job, one esoteric (using Unix commands), and one easy (using cPanel, or some other graphical interface). Even cPanel can be tricky (I had help from the excellent support at AN Hosting to get my own cron jobs working).

Here are some tips designed for beginners.

Getting Started

This tutorial assumes you’re using cPanel, one of the most popular hosting control panels. If you’re on a shared hosting plan, there’s a good chance your host uses cPanel (some popular hosts, like DreamHost and GoDaddy, don’t). But even if you don’t have cPanel, your control panel will probably let you create cron jobs without knowing Unix, in a way similar to what I’ll describe below.

Note:If you’re looking for a host, it’s a good idea to find one that uses cPanel; once you become familiar with it, it’s easier to change hosts if you find out yours sucks and you have to move to another one (be sure to read How to Find a Web Host That Doesn’t Suck).

Let’s get started. Log in to your cPanel and look for the Cron Jobs icon:

cPanel Cron Jobs

Click on the image and you’ll be asked to choose your experience level. If you’re reading this, you want to click on Standard.

Creating a Cron Job

Now that you’re at the Standard Cron Manager, let’s create a cron job. You can set a job to run at a specific interval or at a specific times. Creating a cron job requires only three simple steps (see picture below):

Cron job

  1. Enter your email address where it says: Please enter an email address where the cron output will be sent. Cron will send you a message when the job is executed, so you’ll know if there are any errors.
  2. In the Command to run: field, enter the full path to your script (you may need to check with your host for this).

    If you want to run a php file, the command will begin with php. For example, if you want to run RSS Import to update your Pligg feeds, you’d enter php and the path to your import_feeds.php file.

    On AN Hosting, your path would look something like this:

    php /home/[your username]/public_html/rss/import_feeds.php.

    If you want to run the automatic backup script, which backs up your MySQL database and emails a copy to you (see 8 MySQL Backup Strategies for WordPress Bloggers (And Others), strategy #7), you’d enter /bin/sh (since this is a shell script), and the path to your script.

    On AN Hosting, your path would look something like this:

    /bin/sh /home/[your username]/etc/upstart_cron_backup.sh

  3. Now you need to set the schedule.
    • Set the Minute(s). You can set a short interval here, or minutes of the hour (you can select multiple items in this—and the other— boxes by using the Shift and Command keys). If you don’t care about minutes, leave this set to 0).

      If you want to test things, set this to run Every Five Minutes. This is a good interval for running the script, checking your email for errors, and changing settings before your inbox gets inundated with cron messages.

    • Set the Hour(s). If you’re creating a cron job to run RSS Import, you might want to set this to Every Hour. If you’re setting up a backup script, you might choose an hour in the middle of the night for nightly backups.
    • Set the Day(s). You’ll probably want to leave this set to Every Day, but you can also choose specific days of the month.
    • Set the Weekday(s).
    • Set the Month(s).

When you’ve set the schedule, your cron job is done. Click the Save Crontab button. If you set it to a short interval for testing, you should have an email with your output within a few minutes. If there’s an error (usually a problem with your path), check your settings.

 Stumble it!

All feedback is welcomed via comment or trackback.

Comments and Trackbacks

One Trackback

  1. links for 2007-08-06 added this trackback on 06 Aug 07

    […] How to Create a Cron Job in cPanel (tags: cronjob cpanel cron) […]

36 Comments

  1. Patrix 26 Mar 07

    Any idea how to write a cron command for counting and listing unique outbound links every week/month? Just wanted to see how many unique sites I link to? Or if I am linking to some sites more than others?

  2. Robert Ellis 26 Mar 07

    Sorry, Patrix, I have no idea. But you might try this: SEO Tools » Outbound Links Check.

  3. reneesh t k 16 Apr 07

    Hi,
    Can we change the cron time of cpanel using php program?
    I want to change the time of execution of cron job using php program means the admin of the website want to edit the time of cron job within the admin page of the site. Is it possible.?. He dont want to edit the cpanel.

    Can you help me.

  4. Robert Ellis 16 Apr 07

    I’m not sure I understand what you mean. But if you want to run a cron job from WordPress, instead of using cPanel, it’s possible with a plugin. You can try WP-Cron.

  5. pratik 26 May 07

    Hi Robert Ellis,
    I am very thankful to you because of getting help from you. I am not added cron-jobs yet. but I will put in my site in 2 days.but it will be very useful for me that I think…

    Have you any more idea about creating cron-job. Using Unix-commands?

  6. Robert Ellis 26 May 07

    pratik, you’re very welcome.

    As far as using Unix to create cron jobs, I’m not likely to write about something like that, but here’s an article that might help: Newbie: Intro to cron.

  7. pratik 28 May 07

    Hi Robert Ellis,
    I tried that technique. But I think I was wrong in someway.. because when I try to save cronjob, it will do nothing for me. when I am going to again in cronjob in cpanel saved information is lost…

    I put the mail address in first top box. And then commant to run. I put my files address
    like “php /home/username/public_html/cronfile.php”;

    and selected time is every 5 min. all other I put blank.. and also I tried it for default..

    have you any idea about this?

  8. Robert Ellis 28 May 07

    pratik, most likely the problem is with your path. This can vary depending on your host, so the best thing to do is contact support for your hosting company and confirm that you have the right path.

    Also, I’m not sure what you mean by “all other I put blank”. You should select “Every…” in the other time fields. Every hour, every day, etc. You should get an email with any errors.

    Good luck!

  9. BV 10 Jun 07

    If I want to setup 20 Cron jobs, it seems like I need to do this 20 times. Is there an easier way to setup multiple cron jobs at one go?

    thanks

  10. Robert Ellis 10 Jun 07

    BV, if you want to set up 20 Cron jobs, you’d have to follow these instructions 20 times or manually edit a crontab file. You’ll need to ask tech support for your host to do this.

  11. Contrid 10 Jun 07

    Thank you very much!
    This is a great cronjob article!

  12. BV 10 Jun 07

    Thanks Rob - good article.
    I wish I could have read this article when I started off with cron jobs… this article makes this easy.

    cPanel should make things easier based on this discussion!! :-)

    Cheers
    BV

  13. Abhishek 21 Jun 07

    Hello All,
    I have this cronjob which is set to run every 10 minutes. It simply executes on php file. But execution takes more than 10 minutes..lets say 12 minutes. So in this case I am not getting any email for completion of cron job. So does that mean cron jobs are not running at all ??

    please help me in this,

    Thanks,
    Abhishek
    www.digi-corp.com

  14. Robert Ellis 21 Jun 07

    Abhishek, even if the cron job takes longer to execute than the interval you have set for it to execute, it should still work if things are properly configured. I’d get in touch with support for your host and see if they can help you troubleshoot.

  15. Amit Pathak 11 Jul 07

    Hello Sir,
    I want to setup cron job after 3 day one mail will be sent to the user then mail file is in php file and if we did not get response from the user then again after 14 day again another mail will be sent and if we got respose then there is no need to send another mail days will be counted from the day when he/she enter our request form pls help me i am new on this cron tab setting job

  16. Upstart Blogger 11 Jul 07

    Sorry, Amit. What you’re describing is too complicated for a simple cron job, I think. Your question is beyond my abilities.

  17. kedar 05 Aug 07

    Hi Robert,
    You write very good article of setting cron job. I get a good knowledge because of you.
    But i hav one query, i want to send newsletter mails to more than thousands of email addresses, how can i do this, may i do this using cron job? we may not send these mail at a time because mail server goes down or block.. how can i do this replay me as soon as possible..

  18. Upstart Blogger 06 Aug 07

    @kedar: I’m sorry, but I’m not really sure how best to do this. Most hosts will limit your email to prevent spam. Assuming you’re not sending out spam, your host may offer some kind of email newsletter program that will work. There are numerous free or inexpensive email services that will do this. Try a Google search.

  19. Mukesh 16 Sep 07

    Hi,

    This is wonderful place for setting a cron job. But I have one issue with running a cron job. Say in between while running a cron job, due to any reason, it gets stop or say cron job is not finished successfully. Can we roll back the entire process. In a situation where large data deletion or insert is done this may create problem. Or any other way to overcome this problem.

    Thanks
    Mukesh

  20. MadNut 07 Oct 07

    I have AN Hostinga account also, but in CRON “advance” it has only 1 set of field i need at least 12 how can i add more than 1 CRON command ? its for a paid script i just bought,

  21. Upstart Blogger 07 Oct 07

    @MadNut: Use the standard cron setup. I don’t think there’s a limit to how many you can set up there.

  22. ste 10 Nov 07

    hey thanks for this part

    php /home/[your username]/public_html/rss/import_feeds.php

    i didnt know that, and i will certainly try it

  23. Rohit Gupta 13 Nov 07

    Hi,

    If Cpanel doesn’t contain a cron manager, then how do u create cron jobs, if your php/mysql application is hosted on (a) shared unix/lynix web server (b) windows web server.

    Pls advice

  24. Rebecca 20 Nov 07

    I am trying to set up cron jobs to visit each of my subdomains daily, so they will automatically update themselves. They need to be visited once per day to do this. What command do I use? Rebecca

  25. Upstart Blogger 20 Nov 07

    @Rohit: If you have cPanel, it should have a cron manager. If it doesn’t, you’ll have to use Unix and I can’t tell you how to do that.

    @Rebecca: I have no idea what you’re trying to do. You’ll need some kind of script and run it once a day for each domain. I’m sorry, but again, I can’t help you.

  26. artofnet 29 Nov 07

    Hi,

    Your article is very useful and very helping.
    I’m the one who has helped by your article to set up a cron jobs on my site.
    Once again thanks for great article.
    Cheers…

  27. Rajiv 17 Dec 07

    The information to run the automated php script is very useful

  28. Alfred 26 Dec 07

    thanks a lot, was so irritated trying everything and not getting it to work.
    it needed the php before the command i guess

  29. Anthony 27 Dec 07

    Very useful information.
    I could not understand why my cron job was not running and it was just down to the php in front of the path.
    Looks like that was the problem for a lot of other people as well !

  30. alprof 24 Jan 08

    the corn job it is effected bad when i am writing wrong sentences.
    plz help me sooooooooonas

  31. Demexii 28 Jan 08

    Thanks for the help. I wasn’t sure what I was doing and having someone else explain it was helpful.

  32. John 24 Feb 08

    Having problems setting up a Cron Job for mail list using PHP List for the newsletter and database. Hosting on Cpanel. I have tried several scripts and one works from the command line but not through the Cron Job Settings.
    Any suggestions?

  33. Hasanur 29 Mar 08

    HEllo “Upstart Blogger 07 Oct 07″

    Thanks for your article …

    I have set cron but getting email with error
    in the path box if i dont put this url

    /home/username/public_html/cronjobs/email.php
    then it reads evry line of email.php and notice error every line..

    but if I put this path
    php/home/hasanur/public_html/cronjobs/email.php
    then says “no file in this directory..”

    would you please help me ?

    thanks

  34. Kleidi 09 Apr 08

    Hello man and thank you for the tutorial ;)
    I have a question… i’m using joomla with fireboard forum that didn’t update stats automatically and i need to make it manually or to set a cron jobs. The link that i click to update stats is http://www.mydomain.com/administrator/index3.php?option=com_fireboard&task=recount !
    Can you tell me how to set the cron job for this task?

    Thank you in advance and have a nice day ;)! I’m waiting for your reply!

  35. hammy 21 Apr 08

    thanks a lot it is usefull for a newbie like me to learn something, thanks a lot !

  36. php-web-developer 02 May 08

    Can you tell me how to set the cron job for Advanced (Unix Style)

Post a Comment

Comments are moderated. It may take some time before your comment appears. Please be patient. Comments become the property of Upstart Blogger.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Note: Please post support questions in the Forums. Be sure to read the Upstart Blogger WordPress Themes FAQ before posting. Thanks!

Your email is never published nor shared. Required fields are marked *

*
*

Hosting that doesn’t suck. Use code UPSTARTBLOGGER for 3 free months.

Return to Top