Cron Jobs Guide to OpenCart
Automate your OpenCart recurring work
Contents
What are Cron Jobs?
Cron Jobs are timed tasks (tasks) performed automatically at specific intervals in Unix-like operating systems. "cron" is a daemon processes that runs in the background and performs commands or scripts in a predetermined timeline.
This allows the automation of repetitive tasks without the need for manual intervention, saving time and ensuring their consistent execution.
Why are they important to OpenCart?
For an OpenCart e-shop, cron jobs are extremely useful for automation of various maintenance and operation tasks, such as:
- Regular data update (e.g. currency exchange rates, availability of products from suppliers).
- Automated backups of database and files.
- Create and update sitemap for search engines.
- Send planned emails (e.g. newsletters, reminders of abandoned baskets).
- Cleaning old data (e.g. logs, sessions, abandoned baskets).
- Run tasks required by extensions installed and active on OpenCart.
Using cron jobs can improve the performance, reliability and efficiency of your OpenCart store.
Common OpenCart Work for Automation
- Create Sitemap: Automatically create sitemap. xml for better SEO.
- Update Equals: If you sell in multiple currencies, automatic rate update is crucial.
- Backups: Automate the backup process for the database and files.
- You can see in detail the Backu Driver p
- Cache/Logs Cleaning: Delete old cache files or logs to save space and possibly improve performance.
- Edit abandoned baskets: Send reminder email or data analysis.
- == sync, corrected by elderman == If you use external systems for stock management.
- Extension Work: Many extensions (e.g. for feeds to Skroutz/BestPrice, newsletters) require cron jobs for their operation.
Cron Jobs Configuration
Setting a cron job usually involves setting the schedule and command to be executed. The setting method depends on the hosting provider and control panel you use.
Through cPanel
- Connect to your cPanel.
- In the section "Advanced" (For Advanced) or similar, find the option "Cron Jobs".
- In "Add New Cron Job", you can select common frequency settings (e.g. once a day, twice an hour) or set custom values for minute, hour, day, month, day of the week.
- In the "Command" field, enter the full command you want to execute. For example, for a PHP script:
/usr/local/bin/php /home/your_cpanel_user/public_html/path/to/your/script.phpReplace it
/home/your_cpanel_user/public_html/path/to/your/script.phpwith the actual path to your script and the/usr/local/bin/phpwith the right path of PHP CLI interpreter on your server (you can learn it from your provider). - Click on "Add New Cron Job".
Through Plesk
- Connect to Plesk.
- Go to "Websites & Domains" and choose your domain.
- On the right side bar or under "Developer Tools", find the option "Scheduled Tasks" (Scheduled Jobs) or "Cron Jobs".
- Click on "Add Task" (Addition of Work).
- Select the work type:
- Run a PHP script: Enter the path to your PHP script (e.g.,
/httpdocs/path/to/your/script.php). You can also set the PHP version. - Fetch a URL: Enter the URL you want to call magazines.
- Run a command: Enter full command (similar to cPanel).
- Run a PHP script: Enter the path to your PHP script (e.g.,
- Set the timeline using the cron syntax or default options.
- You can set description and notifications.
- Click on "OK" or "Run Now" for immediate testing and storage.
Through Command Line (SSH)
If you have SSH access to your server, you can manage the cron jobs directly using the command crontab.
- Connect to server via SSH.
- To edit the cron jobs of the current user, type in:
crontab -e - This will open the crontab editor (usually nano or vi). Add a new line for each cron job with the following syntax:
# Minute Hour DayOfMonth Month DayOfWeek Command # (0-59) (0-23) (1-31) (1-12) (0-7, 0 or 7 is Sun) 0 2 * * * /usr/bin/php /var/www/html/opencart/cron_script.php >/dev/null 2>&1This example performs the
cron_script.phpEvery day at 2:00 a.m.>/dev/null 2>&1suppress the exit of the order. - Save and close the file. The changes are being implemented immediately.
- To see the existing tasks:
crontab -l
Special Settings for OpenCart
OpenCart from version 4.x onwards, includes an integrated mechanism for managing planned tasks (Scheduled Tasks). You can find him in the management menu: System > Maintenance > Scheduled Tasks.
To operate this mechanism, you need to set up a single cron job on your server that calls the file system/cron.php or the corresponding URL. The command is usually:
wget -O /dev/null -q "https://to_site_sas.gr/system/cron.php?cron_token=YOUR_CRON_TOKEN" >/dev/null 2>&1 Or using PHP CLI:
/usr/bin/php /path/to/your/opencart/system/cron.php --cron_token=YOUR_CRON_TOKEN >/dev/null 2>&1 The YOUR_CRON_TOKEN you can find it/set it in System > Settings > Edit Store > Server (tab) > Cron Token. This cron job needs to run frequently (eg every 5-15 minutes) so that OpenCart can run its own scheduled tasks on time.
For older OpenCart versions or for custom scripts, you will need to create your own PHP scripts and call them through cron jobs as described above.
Cron Scripts Protection
If you call PHP scripts directly via URL, make sure they are protected. You can add an IP check, a token secret to the URL, or configure the web server to allow access only from localhost.
Examples of Cron Commands
The exact syntax may vary depending on your server.
- Run PHP script every day at 3:30 a.m.:
30 3 * * * /usr/bin/php /home/user/public_html/opencart/to_script_sas.php - URL call every hour (e.g. for OpenCart Scheduled Tasks):
0 * * * * wget -O /dev/null -q "https://to_site_sas.gr/system/cron.php?cron_token=SECRET_TOKEN"Alternative to
curl:0 * * * * curl --silent "https://to_site_sas.gr/system/cron.php?cron_token=SECRET_TOKEN" > /dev/null 2>&1 - Database backup (see the Backu Driver p for details:
0 2 * * * /usr/bin/mysqldump -u db_user -p'db_password' db_name | gzip > /home/user/backups/db_backup_`date +\%Y-\%m-\%d`.sql.gz
Watch the routes!
Make sure you use the absolute routes for the executable files (php, wget, mysqldump) and your scripts. You can find the path of an executable with the command which php (or which wget etc.) in SSH terminal.
Best Practices
- Frequency: Do not set cron jobs to run more often than necessary. This can burden the server.
- Fortu distribution: If you have many cron jobs, try to distribute them time so that they do not all run simultaneously.
- Logging: For important scripts, add a logging mechanism within your script to record the execution and any errors. You can also redirect the cron job output to a file:
- Notifications: Configure email notifications for cron jobs failures, if your control panel supports it, or integrate the email sent logic into your script for critical errors.
- Security: Protect your cron scripts from unauthorized access, especially if called through URL.
- Cleaning: Remove the cron jobs no longer required.
- Test: Before you activate a cron job in a productive environment, try the script manually to make sure it works properly.
* * * * * /path/to/command >> /path/to/cron.log 2>&1 Dealing with Problems
- Cron Job is not running:
- Check the syntax of the cron schedule.
- Make sure the command is correct and uses absolute routes.
- Check the script execution rights.
- Check system logs (e.g.
/var/log/cronor/var/log/syslog) for error messages. - Make sure Cron daemon runs to the server.
- Script is running but not working properly:
- The running environment of a cron job may differ from the running environment via web server or manually from terminal (e.g. different environment variables, different user).
- Add detailed logging to your script to locate the problem.
- Check for PHP errors if the script is PHP. You can enable PHP error log in the script or in
php.ini.
- Download Email from Cron Jobs: If you receive emails every time a cron job is performed, this is because the script produces an output (output). To avoid it, redirect the exit to
/dev/null:* * * * * /path/to/command >/dev/null 2>&1
Automate your OpenCart shop today!
Contact us for specialized assistance in cron jobs configuration and automation of your e-shop processes.