+1-855-211-0932 (ID:287497)
Don't have an account yet? Sign up now!

HomeArticlesHow to Set Up a Cron Job

How to Set Up a Cron Job

How to Set Up a Cron Job

How to Set Up a Cron Job

Category: Tools & Settings

What is a Cron Job?

A cron job is a scheduled task that runs automatically at specified intervals on your server. It can execute shell commands or scripts to perform tasks like backups, updates, or sending newsletters, which are essential for maintaining your script-based websites.

Why Use Cron Jobs?

Cron jobs are particularly useful for:

  • Automating repetitive tasks without manual intervention.
  • Maintaining your website's performance and functionality.
  • Running scripts that manage databases, send emails, or perform regular updates.

Understanding Cron Job Formatting

Cron jobs are formatted into specific fields that define the schedule and command. The general format is as follows:

Cron job time schedule - [ Minute - Hour - Day - Month - Weekday ]
Cron job shell command - [ARGUMENTS]
Cron job script command - [PATH OF PHP/PERL] [ARGUMENTS] [PATH OF PHP SCRIPT]
            

Cron Job Time Schedule Operators

Here are the common operators you can use for scheduling:

.---------------- minute (0 - 59)
| .------------- hour (0 - 23)
| | .---------- day of month (1 - 31)
| | | .------- month (1 - 12) OR jan,feb,mar,apr ...
| | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
| | | | |
* * * * * command to be executed
            

Examples of Cron Jobs

Below are a few examples of cron job scheduling:

  • * * * * * => Execute every minute
  • 0 * * * * => Execute every hour
  • 0 0 * * * => Execute at midnight
  • 0 0 1 * * => Execute on the first of every month
  • 0 0 * * 1 => Execute every Monday

Creating Cron Jobs from Control Panel

To create a cron job, follow these steps:

  1. Navigate to Advanced > Cron Jobs in your Control Panel.
  2. Click on the Create a New CronJob button.
  3. Specify your email address for result notifications, the date and time for execution, and the command to run.

Example Commands for Cron Jobs

Here are some examples of shell command and script execution cron jobs:

# Delete all files in the temp directory every hour
0 * * * * rm -f /home/www/my-best-domain.com/temp/*

# Send newsletters daily at midnight
0 0 * * * /usr/bin/php /home/www/my-best-domain.com/mailcron.php
            

Conclusion

Cron jobs are a powerful tool for automating tasks on your website. By setting them up correctly, you can ensure that essential maintenance tasks run smoothly without manual intervention.

For more detailed information on allowed file types and extensions for cron jobs, refer to our tutorial on Files that could be executed with crontab - allowed file extensions.



Tags: , , , , , , , , ,

Post a Comment

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

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>