Cron Job Basics
This article offers an overview of what Cronjobs are. If you're unfamiliar with Cronjobs, please read on carefully, as they represent a more advanced feature of your account. For those already acquainted with Cronjobs and seeking instructions on their use, please refer to the following list of Cronjob articles:
- What is a Cronjob?
- Cronjob Limits
- Cronjob Environment
- Setup, Remove or Edit a Cronjob
- Cron Running at Specific Date and Time
- Specify a Cronjob to use a Specific php.ini File
- Cronjobs with PHP and other File Types
What is a Cronjob?
A Cronjob is a Linux command for scheduling a task (command). Cronjobs allow you to automate specific commands or scripts on your server to complete repetitive tasks automatically.
This can be a very resourceful tool as a Cronjob can be set to run by 15 minute or hourly increments, a day of the week or month, or any combination of these. Bluehost limits Cronjobs on shared servers to run in increments no less than every 15 minutes as excessive Cronjobs can cause unnecessary loads on the server.
For example, you could set a Cronjob to delete temporary files every week so that unnecessary files are not using up your disk space. Some scripts, such as Drupal, may even require you to set up a Cronjob to perform certain functions.
Sample Cronjobs Commands
- PHP command examples:
/usr/local/bin/php /home4/cemmlfmy/public_html/path/to/cron/script
- Domain-specific example::
/usr/local/bin/ea-php99 /home4/cemmlfmy/domain_path/path/to/cron/script
Scheduling in Cronjobs
To understand the settings in a cronjob, you need to know how its scheduling works. A cronjob is scheduled based on five time units represented by different fields. Here's what you need to know about these units:
- Minute - Ranges from 0 to 59.
- Hour - Ranges from 0 to 23 (24-hour format).
- Day of the month - Rangers from 1 to 31.
- Month - Ranges from 1 to 12 (January to December).
- Day of the week - Ranges from 0 to 6 (Sunday to Saturday).
Warning: It is important that you familiarize yourself with how Cronjob works. You need to have a good knowledge of Linux commands before you can use Cronjob effectively.
Check your script with your hosting administrator before adding a Cronjob.
For most Cronjobs, there are three components present:
- The script that is to be called or executed.
- The command that executes the script on a reoccurring basis. This is typically set in the cPanel.
- The action or output of the script, which depends on what the script being called does. Frequently, scripts are called a Cronjob to modify files or databases; however, they can perform other tasks that do not modify data on the server, like sending out email notifications.
Note: Most scripts that require a Cronjob will give you specific instructions on what needs to be set up, frequently giving examples.
Cronjob Limits
Shared Hosting
Important: You may not run a Cronjob more often than every 15 minutes.
If you are getting errors when using the GET command, please contact us and request access.
VPS and Dedicated Server
You may run a Cronjob as often as you desire. We enable GET and WGET by default
Cronjob Environment
Sample Cronjob Page in cPanel:
- The initial working directory for the process is your home directory.
- Because the processes are run in a non-interactive shell, .bashrc and other startup files are not read.
- Only /usr/bin and /bin will be checked for the specified executable. Executables in other directories must be specified as absolute paths, such as /ramdisk/bin/php5, or relative paths, such as ./my_program.
- When running a script, the interpreter must be specified either in a valid #! line using a full path or at the command line.
- File arguments must be specified with absolute paths or paths relative to the current working directory.
Summary
Cronjobs are invaluable tools for automating server tasks, executing commands or scripts at scheduled intervals. They play a vital role in efficient server management, allowing users to set intervals ranging from minutes to months. However, it's crucial to grasp Linux commands and adhere to server specifications. Bluehost, for instance, enforces a 15-minute minimum interval on shared servers to prevent overload. While different server types have varied restrictions, ensuring scripts are correctly configured is paramount. By understanding and effectively utilizing Cronjobs, users can streamline operations and optimize server performance with ease.
If you need further assistance, feel free to contact us via Chat or Phone:
- Chat Support - While on our website, you should see a CHAT bubble in the bottom right-hand corner of the page. Click anywhere on the bubble to begin a chat session.
- Phone Support -
- US: 888-401-4678
- International: +1 801-765-9400
You may also refer to our Knowledge Base articles to help answer common questions and guide you through various setup, configuration, and troubleshooting steps.