Crontab is a time-based job scheduler that helps you to run scripts automatically at a certain time or date and it is commonly used to automate system maintenance or administration.
Here is an example of a command that calls for a crontab script under a bereveal.com on version PHP 5.2:
/usr/local/php5/bin/php /home/www/domain.com/crontab.php
You can see a list of all command paths for the different versions of PHP, Perl and Python in the Help section
Related help topics
-
What is crontab?
The Cron Daemon runs on Unix-based Operating Systems and, in essence, is closely similar to the Windows Task Scheduler.
It allows you to post your own rules for useful and time sparing automation of any of the scripts you will be working with.
Cron is what enables users to schedule jobs, which run automatically at a certain date or time and thus perform system administration or script-related tasks.
The crontab is the virtual scoreboard for setting up cron jobs and represents a configuration file to which users write shell commands that run periodically, at a given schedule.
The Cron Daemon is a long running process that executes commands and performs scheduled tasks at a specific date or time.
The stored commands are what we call cron jobs and the utility, which is used to keep a record of them is Crontab.
For your convenience, we have created a visual interface for creating cron jobs.
-
Why should I use cron jobs?
Server admins have been using cron jobs for a long time. But since the target audience of this article is web developers, let's look at a few use cases of cron jobs that are relevant in this area:
- If you have a membership site, where accounts have expiration dates, you can schedule cron jobs to regularly deactivate or delete accounts that are past their expiration dates.
- You can send out daily newsletter e-mails.
- If you have summary tables (or materialized views) in your database, they can be regularly updated with a cron job. For example you may store every web page hit in a table, but another summary table may contain daily traffic summaries.
- You can expire and erase cached data files in a certain interval.
- You can auto-check your website content for broken links and have a report e-mailed to yourself regularly.
- You can schedule long-running tasks to run from a command line script, rather than running it from a web script. Like encoding videos, or sending out mass e-mails.
- You can even perform something as simple as fetching your most recent Tweets, to be cached in a text file. -
How to create a cron job?
To set up a cronjob you will need to press the "Create A New Cronjob" button and set a few parameters: time schedule, execution command and the email where the cronjob result notifications will be sent to.
Experience level - you can choose between Standard or Advanced level of experience - this is how you want to set up the time schedule.1. Here is an example of cron job time scheduling:
***** command to be executed
| | | | |
| | | | |
| | | | ----- day of week (1 - 7) (Monday = 1)
| | | ------- month (1 - 12)
| | --------- day of month (1 - 31)
| ----------- hour (0 - 23)
------------- min (0 - 59)* * * * * -> Execute every minute
*/5 * * * * -> Execute every 5 minutes
0 * * * * -> Execute every Hour
0 */5 * * * -> Execute every 5 Hours
0 0 * * * -> Execute every day at Midnight
0 0 0 * * -> Execute every Month
0 0 0 0 * -> Execute every Weekday2. Command to be executed
Basically, if you need to set up a cronjob for an application on your website, you will be given the exact command and the URL which you need to execute. Just in case, here are a few examples as an illustration:
- Delete files within a certain folder in your account with the following command:
rm -f * /home/www/my-best-domain.com/testfolder/
This will delete all files located in /testfolder and the "-f" stands for "force" and deletes the files without asking for permission
- Run a script, for example that will be sending emails on certain time schedule:
/usr/local/php5/bin/php /home/www/my-best-domain.com/mail.php
Here is an example of a command that calls for a crontab script under a certain host on version PHP 5.2:
/usr/local/php5/bin/php /home/www/domain.com/crontab.php
Here is a list of all command paths for the different versions of PHP, Perl and Python:
/usr/local/php4/bin/php
/usr/local/php5/bin/php
/usr/local/php5.3/bin/php
/usr/local/php5.4/bin/php
/usr/local/php5.5/bin/php
/usr/local/bin/python2
/usr/local/bin/python3
/usr/bin/perl
-
How to disable the Crontab email alert ?
If you have, for example, a cronjob executed every 2 minutes, this means that you will receive an email with the cron result every 2 minutes.
To prevent the sending of email notification, add any one of the following lines at the end of each cronjob command:>/dev/null 2>&1
or
&> /dev/nullExample:
/usr/bin/php /home/www/my-best-domain.com/mail.php >/dev/null 2>&1
or
/usr/bin/php /home/www/my-best-domain.com/mail.php &> /dev/null