How to set up Cronjob for your site
Understanding Cronjobs
Cronjobs are scheduled tasks that run automatically at predefined intervals on a server, they're commonly used for automating repetitive tasks. Cronjobs are essential for maintaining the functionality and efficiency of web applications.
Example Applications of Cronjobs
- Sending reminder emails: Utilize cronjobs to send reminder emails to customers who have abandoned their carts, promoting the completion of their purchases (e.g., with the "Abandoned Cart Reminder" module).
- Automated customer notification: Set up cronjobs to automatically send emails to customers when their rewards are about to expire, fostering engagement and loyalty (e.g., with the “Loyalty, referral & affiliate programs” module).
- Cache management: Schedule cronjobs to clear expired cache files, ensuring optimal website performance (e.g., with modules like "Super Speed" or "Page Cache Pro").
- Automated tasks: Use cronjobs to automatically send notification emails and automatically delete expired discount codes and/or used discount codes, enhancing the overall user experience (e.g., with the “Product Reviews” module).
Understanding Cronjob Syntax
Cron jobs are defined using a specific syntax that dictates the frequency and timing of their execution. This syntax consists of five fields, each represented by an asterisk (*) or a specific value:
Minute (0-59): Defines the minute within an hour when the cron job should run. An asterisk (*) represents all minutes.
Hour (0-23): Specifies the hour within a day when the cron job should run. Similar to minutes, an asterisk (*) indicates all hours.
Day of the Month (1-31): Defines the day of the month when the cron job should run. An asterisk (*) represents all days.
Month (1-12): Specifies the month in which the cron job should run. An asterisk (*) indicates all months.
Day of the Week (0-6): Defines the day of the week when the cron job should run. 0 and 7 represent Sunday, while 1-6 represent Monday to Saturday. An asterisk (*) indicates all days of the week.
Common Symbols for Cronjob Setup
- Asterisk ( * ): The asterisk means "every" and matches all values of the field. For instance, using * in the minute field indicates every minute.
- Slash ( / ): Slashes represent increments of ranges. For example, 3-59/15 in the minutes field means the third minute of the hour and every 15 minutes thereafter.
- Comma ( , ): Commas separate items in a list. For instance, 1,2,5 in the day of the week field means Mondays, Wednesdays, and Fridays.
- Hyphen ( - ): Hyphens define ranges. For example, 2000-2010 includes every year between 2000 and 2010.
- L: “L” stands for "last." In the day-of-week field, it allows you to specify constructs like "the last Friday" (5L) of a month. In the day-of-month field, it specifies the last day of the month.
- W: The “W” character finds the nearest weekday (Monday-Friday) to the given day. For example, specifying "15W" as the day-of-month value means "the nearest weekday to the 15th of the month".
- Hash ( # ): “#” is for the day-of-week field and must be followed by a number between one and five. For example, 5#2 indicates "the second Friday" of a month.
- Question mark ( ? ): It is used instead of “ * ” for leaving either day-of-month or day-of-week blank. In practice, “ ? ” works the same as '' * ”.
Example Cronjob Configurations
To illustrate the concept of cron job syntax, let's consider some practical examples:
Run a task every minute:
* * * * * php /path/to/script.php
Here, the asterisk (*) in each field indicates that the task should run every minute of every hour, every day of every month.
Run a task every 5 minutes:
* /5 * * * php /path/to/script.php
Here, the asterisk (*) in each field indicates that the task should run every minute of every hour, every day of every month.
Run a task at 2:00 PM every day:
0 14 * * * php /path/to/script.php
In this example, the '14' in the second field specifies that the task should run at 2:00 PM (14:00 hours). The rest of the asterisks indicate that the task should run every day of every month.
Run a task on the first Friday of every month:
0 5 1 * FRI php /path/to/script.php
Here, the '5' in the second field indicates that the task should run at 5:00 AM (05:00 hours). The '1' in the third field defines that the task should run on the first day of the month. The 'FRI' in the fifth field specifies that the task should run only on Fridays.
If your cronjob setup seems complex and you're unsure how to configure it, let AI assist you. Use the sample prompt below with ChatGPT, Germini, Bing, or any other AI tool to generate the cronjob command for you:
I want to set up a cronjob command to run a task at {The time you need to run cronjob}. Help me to write that cronjob command.
Example:
I want to set up a cronjob command to run a task at 06:00 on the 1st day and 15th of each month. Help me to write that cronjob command.
How to Set Up a Cronjob
Most hosting providers offer a cPanel or Plesk administration interface.
- In cPanel, go to "Crontab" in the "CWP Settings" section. Click on “Add new cron job” button.
- Enter the cronjob information, including:
- Command: Cron command to execute the desired task.
- Schedule: Select cronjob execution frequency.
- Now save your cron then click on the “Run” icon to test your configured cronjob.
In the screenshot below, we using a demo of the “Abandoned Cart Reminder” module:
If your server allows custom commands, you can copy the whole command (with 5 stars ahead – that means run once per minute).
- In Plesk, go to "Tools & Settings" > "Scheduled Tasks".
- Click “Add task” and specify the “Run” parameter to “Cron style”.
- Fill in the “Run” text field with cron-style time syntax, for example, * */4 * * *
- Fill in the “Command” text field with the cron command
- Click on the “OK” button to save your cron.
How To Test Cronjob
1. Test cronjob manually
For each module developed by PrestaHero that features cronjob functionality, we provide the option to execute the cronjob manually. If you wish to test the cronjob command manually, simply click on the "Execute cronjob manually" button located directly below the cronjob command provided by the module.
After running manually, if you see the last time cronjob was executed a few seconds ago - that means the cronjob configuration on the server is correct.
Or if your server provides the “Run cronjob to test” feature, you can test your cronjob and see the result by clicking on the “Run cronjob” button.
This manual testing option is convenient for developers and administrators who want to validate cronjob functionality, troubleshoot potential issues, and ensure smooth operation before relying on automated scheduling.
2. Enable cronjob log
PrestaHero modules that provide the Cronjob feature will also provide the Cronjob logs feature. You can monitor the activity log of the cron command you installed here to know whether the cronjob has executed correctly or not.
Please enable the "Save cronjob logs" feature of the respective module and monitor the logs in the "Cronjob log" field. In the screenshot below is the Cronjob activity log of the "Abandoned Cart Reminder" module:
Notes:
- Be careful when entering cronjob information; even minor errors can prevent cronjob from working.
- It is recommended to test the cronjob thoroughly before using it for important tasks.
- Use the cronjob log to track and troubleshoot cronjob errors.
If you encounter difficulties locating or configuring cron jobs on your server, our dedicated support team is always ready to assist you. Simply reach out to our support team for expert guidance and ensure seamless cron job setup.