Chapters Close

This article will help you to understand how to create or remove your Magento Crontab.

Create Cron using CLI

Starting with version 2.2, Magento creates a crontab for us. Run cron as the Magento file system owner.

  1. Log in as, or switch to, the Magento file system owner in the terminal.
  2. Change to your Magento installation directory: cd <your Magento install dir>/
  3. Enter the following command: php bin/magento cron:install [--force] Here, use –force to rewrite and Magento crontab.

The Magento crontab is inside #~ MAGENTO START and #~ MAGENTO END comments in your crontab.

  • magento cron:install does not rewrite an existing crontab inside #~ MAGENTO START and #~ MAGENTO END comments in your crontab.
  • magento cron:install --force has no effect on any cron jobs outside the Magento comments.

To view the crontab, enter the following command as the Magento file system owner in terminal:

Example:

#~ MAGENTO START
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento cron:run 2>&1 | grep -v Ran jobs by schedule >> /var/www/html/magento2/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/html/magento2/update/cron.php >> /var/www/html/magento2/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/html/magento2/bin/magento setup:cron:run >> /var/www/html/magento2/var/log/setup.cron.log
#~ MAGENTO END

Remove Cron using CLI

To clear cron schedule in Magento 2:

  1. Log in as, or switch to, the Magento file system owner in the terminal.
  2. Change to your Magento installation directory: cd <your Magento install dir>/
  3. Enter the following command: php bin/magento cron:remove

This command has no effect on cron jobs outside the #~ MAGENTO START and #~ MAGENTO END comments in your crontab.

Run Cron from CLI

Command options:

php bin/magento cron:run --group index
php bin/magento cron:run --group default

php bin/magento cron:run [–group=""] where --group specifies the cron group to run.
Example :

Hope it helps you! 🙂

FAQs

What is * * * * * In cron job?


The * * * * * in cron job indicates the run schedule of the job. Each asterisk corresponds to the minute, the hour, the day, the month, and the year at which the job should repeat. You can replace each of the * with a number (0-59 for minutes, 0-12 for hours, and so on) to create a schedule for the job. If you use the expression “* * * * *” the job will run every single minute of every hour.

How do I disable cron job in Magento?


To disable the cron job in Magento 2, use the command line – php bin/magento cron:disable. It will update the cron_schedule table and set the status field to error for all scheduled cron jobs. Alternatively, you can run the command crontab -e and comment out the line that starts with * * * * * and ends with php.

How to create custom cron in Magento 2?


To create a custom cron in Magento 2, follow the below-mentioned steps –

1. Create a new module in the app/code directory of your Magento installation.
2. Next, create a new crontab.xml file in the module’s etc directory.
3. In the above file, use the following code to define your custom cron job:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="vendor_module_custom_cron" instance="Vendor\Module\Cron\CustomCron" method="execute">
<schedule>*/5 * * * *</schedule>
</job>
</group>
</config>

4. Further, create a new class in the Cron directory of your module as CustomCron.php.
5. Now, in the above file, define the execute method by adding the below code –
<?php
namespace Vendor\Module\Cron;
class CustomCron
{
public function execute()
{
//Add your cron job logic here
}
}

6. Thereafter, run the command php bin/magento setup:upgrade in your Magento root directory to enable the module.

How to check cron is enabled in Magento 2?


To check cron is enabled in Magento 2, use the command line – php bin/magento cron:status in CLI. Make sure to run it in your Magento root directory! This command will return the status of the cron jobs and will only indicate whether or not cron is enabled.

In this blog post, I will guide you on how to change the Magento 2 configuration from the CLI command. So without wasting much time, let’s dive into the post.

We can set any configuration options from the command line.

  • Before installing Magento, one can set the configuration values for the default scope only, which is the only valid scope.
  • After installing  Magento, You can set the configuration values for any website or store view scope which is quite beneficial.

We use the commands given below to set the Magento 2 configuration:

  • bin/magento config:set sets any non-sensitive configuration value by its configuration path.
  • bin/magento config:sensitive:set sets any sensitive configuration value by its configuration path.
  • bin/magento config:show shows saved configuration values; values of encrypted settings that are displayed as asterisks.

To set a configuration value, you must know at least one of the following:

  • The configuration path
  • The scope code, to set a configuration value for a particular scope

Set configuration values

  • To set system-specific configuration values on Magento 2.2.0 – 2.2.3, use: $ bin/magento config:set [--scope="..."] [--scope-code="..."] [-l | --lock] path value
  • To set system-specific configuration values on Magento 2.2.4 and higher, use: $ bin/magento config:set [--scope="..."] [--scope-code="..."] [-le | --lock-env] [-lc | --lock-config] path value
  • To set sensitive configuration values, use: $ bin/magento config:sensitive:set [--scope="..."] [--scope-code="..."] path value
    • The config:sensitive:set command writes sensitive settings to <Magento base directory>/app/etc/env.php.

–scope : Defines scope of the configuration. Valid values are default, website, or store. The default value is set as default.

–scope-code: Defines scope code of configuration (website code or store view code).

-l or –lock (Version: 2.2.0 – 2.2.3) : Either locks the value so it cannot be edited in the Magento Admin or changes a setting that is already locked in the Magento Admin. The command writes the value to the <Magento base dir>/app/etc/env.php file.

-le or –lock-eav (Version 2.2.4 and higher) :  Either locks the value so it cannot be edited in the Magento Admin or changes a setting that is already locked in the Magento Admin. The command writes the value to the <Magento base dir>/app/etc/env.php file.

-lc or –lock-config(Version 2.2.4 and higher) : Either locks the value so it cannot be edited in the Magento Admin or changes a setting that is already locked in the Magento Admin. The command writes the value to the <Magento base dir>/app/etc/config.php file. The –lock-config option overwrites –lock-env if you specify both the options.

Path : Required. The configuration path.
Value : Required. The value of the configuration.

Example:

  1. Set Sender Email for Sales Representative with default scope:
    • $ bin/magento config:set trans_email/ident_sales/email sales@aureatelabs.com
  2. Set the Sender Email for Sales Representative for the base website:
    • $ bin/magento config:set --scope="website" --scope-code="base" trans_email/ident_sales/email aureate_sales@aureatelabs.com
  3. Set the Sender Email for Sales Representative for the test store view:
    • $ bin/magento config:set --scope="stores" --scope-code="default" trans_email/ident_sales/email aureate_sales@aureatelabs.com

Set configuration values that cannot be edited in the Magento Admin

If you have used –lock-eav (version 2.2.4 or higher) or –lock (version 2.2.0-2.2.3) option to set Magento configuration, this command will save the configuration value in <Magento base dir>/app/etc/eav.php file and will disable the config field for further editing the value in the admin.

$ bin/magento config:set --lock-env --scope=stores --scope-code=default trans_email/ident_sales/email sales@aureatelabs.com

If you have used –lock-config (version 2.2.4 or higher) option to set Magento configuration, this command saves configuration value in <Magento base dir>/app/etc/config.php file and disables the config field for edit value in the admin.

$ bin/magento config:set --lock-config --scope=stores --scope-code=default web/url/use_store 1

Display the value of configuration settings

$ bin/magento config:show [--scope[="..."]] [--scope-code[="..."]] path

Example:
$ bin/magento config:show --scope="stores" --scope-code="default" web/url/use_store

Result :
1

Show all saved configuration

$ bin/magento config:show

Result :
web/seo/use_rewrites - 1
web/unsecure/base_url - http://127.0.0.1/magento/
web/unsecure/base_static_url -
web/unsecure/base_media_url -
web/secure/base_url - https://127.0.0.1/magento/
web/secure/use_in_frontend - 0
web/secure/use_in_adminhtml - 0
web/secure/base_static_url -
web/secure/base_media_url -
.
.
.

Show all saved configuration for the base website

$ bin/magento config:show --scope="website" --scope-code="base"

Result :
web/unsecure/base_url - http://127.0.0.1/magento/
trans_email/ident_sales/email - aureate_sales@aureatelabs.com
.
.

Show Sender Email of Sales Representative configuration for default scope

$ bin/magento config:show trans_email/ident_sales/email

Result :
sales@aureatelabs.com

Show Sender Email for Sales Representative configuration for the base website

$ bin/magento config:show --scope="website" --scope-code="base" trans_email/ident_sales/email

If the configuration value is not set for the website, then it will give an error as shown below:
Configuration for path: "trans_email/ident_sales/email" doesn't exist

Hope this detailed article served your purpose. Feel free to leave the comments below and contact us if you need any help to customize your Magento store.

Thanks for your interest.

FAQs

What is CLI command in magento 2?


The CLI command in Magento 2 stands for Command Line Interface. The interface allows you to perform a variety of tasks including commerce installation, updates, clearing cache memory, and other such configuration tasks utilizing commands. You can also add, modify, or list all available commands through the command line interface.

How do I access magento from command line?


To access Magento from command line, follow the below steps:
1. Open the command prompt on the Magento server.
2. Redirect the directory to the folder where Magento is installed. 
3. Run the command “chmod 744 bin/magento” to execute the CLI.
4. Once the CLI is executable, utilize bin/magento to use the CLI and also list all the commands.

How to make a cli command in magento 2?


To make a CLI command in Magento 2, follow these steps –

1. Create a new module in the app/code directory of your Magento installation.
2. In the module’s etc directory, create a new module.xml file.
3. Add the following code to register the module with Magento:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0"/>
</config>

4. Now, in the module’s Console directory, create a new Command class, and it must implement the configure() & execute() methods. 
<?php
namespace Vendor\Module\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CommandName extends Command
{
protected function configure()
{
$this->setName('vendor:commandname')
->setDescription('Command Description');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
//Add your command logic here
$output->writeln('Command executed successfully.');
}
}

5. Further, create a new di.xml file in the module’s etc directory by adding the code –
?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
            <argument name="commands" xsi:type="array">
            <item name="vendor_module_commandname"
             xsi:type="object">Vendor\Module\Console\CommandName</item>
</argument>
</arguments>
</type>
</config>

6. Run the command php bin/magento setup:upgrade in your Magento root directory to enable the module.
7. Thereafter, run the command php bin/magento vendor:commandname to execute the command.

What is the correct way to execute magento CLI commands?


The correct way to execute the Magento CLI command is by using php bin/magento <command> in the root directory. For example, to run the command cache:clean, use php bin/magento cache:clean. You can also use the list command php bin/magento list to retrieve all the available other commands.

It’s PHP 7.2 that is trending these days and we will make sure that you are always up to the trend mark and never behind! In this article, you will be learning ways in which you can check if your custom Magento code is compatible with PHP version 7.2 or not.

Step 1 : Set up PHP_CodeSniffer and PHPCompatibility

To start with, you need to set up PHP_CodeSniffer on the system. You can set it up using the link given below:
https://github.com/squizlabs/PHP_CodeSniffer

After successful installation of PHP_CodeSniffer starts installing PHPCompatibility on the system using the following link.
https://github.com/PHPCompatibility/PHPCompatibility

Note: PHPCompatibility with version > 8.0 will only work with PHP version 7.1. Do make sure you have installed PHP version 7.1 on your system.

Step 2 : Execute commands to check compatibility

Now, to proceed further you need to execute commands to check the compatibility.

Go the desired folder (which needs to checked) using the command line and execute the command given below:

phpcs -p . --standard=PHPCompatibility --runtime-set testVersion 7.2

Note: In the above command you can change testversion if you want to check with a different version of PHP.

Above command will give its result as an output in the console itself. If you want to save the result in a specific result file you can do it with the following command:

phpcs -p . --standard=PHPCompatibility --runtime-set testVersion 7.2 --report-full=<Full path of result file>

FAQs

What is PHP compatibility?


PHP compatibility is the ability of a code to run with the support of a particular PHP version. New PHP versions and even patches support different features which limits the ability of certain scripts to run with newer or older versions. You can check the compatibility of a particular script using the command php -l before deploying it.

Is Magento 2 compatible with PHP 8?


No, Magento 2 is not compatible with PHP 8. However, the Magento community is working to develop a Magento codebase that is compatible with the version of PHP. The project is unofficially labeled as Magento PHP 8 Compatibility Community Project and works on crowd contribution to onboard Magento 2 to PHP 8.

Does Magento use PHP?


Yes, Magento uses PHP as its programming language. It is an open-source platform whose server-side code is developed through various PHP frameworks such as Laminas, Symphony, and Zend. Though it also uses other languages such as MySQL, the code is majorly based on PHP alone.

Does Magento 2.4 support PHP 8?


Yes, magento 2.4 supports PHP 8. The main feature of the Magento 2.4 version was its ability to run on PHP 8. This is also the official version of Magento 2 that is compatible with PHP 7.4 and above as earlier Magento 2 versions were made compatible only with a community-sourced project.

The title “setup script” itself demonstrates that script files are utilized to execute some operations on the data or the table while installing or upgrading a module.

You can find all the setup scripts files in a Setup folder which is located at: 

app/code/<Vendor name>/<Module Name>/Setup

Let’s dig deep and master more about what recurring script is and how it is helpful. The recurring script is a new trait of Magento 2 that can be created in your module in Setup/Recurring.php

It serves the same purpose as of an Upgrade script but doesn’t have a version comparison condition. An upgrade script only runs when the module version changes whereas a recurring script can run whenever the CLI calls command: $ bin/magento setup: upgrade

Recurring Schema event

Your module’s recurring schema event class is executed by Magento post each schema installation or upgrade stage. This class makes final changes to the database schema after it has been installed or updated.

Well, one thing about recurring scripts is that it does not implement its interface type, instead, they make use of InstallSchemaInterface and the rest of the code is inside the install function.

In the event that you want to execute any operation on the table or data after installing or upgrading any module, you have to generate the recurring script.

For example,
Setup/Recurring.php

<?php

namespace Aureatelabs\Test\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Stdlib\DateTime\DateTime;

class Recurring implements InstallSchemaInterface
{
	/**
	 * @var \Magento\Framework\Stdlib\DateTime\DateTime
	 */
	protected $dateTime;

	public function __construct(
		DateTime $dateTime
	) {
		$this->dateTime = $dateTime;
	}

	public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
	{
		$setup->startSetup();
		$setup->getConnection()->query("INSERT INTO aureatelabs_logs SET log_datetime = '" . $this->datetime->gmtTimestamp() . "'");
		$setup->endSetup();
	}
}

In the illustration given above, while $ bin/magento setup:upgrade command is called in CLI the new record will be entered in the log table (aureatelabs_logs).

There’s hardly one example in the prevailing Magento 2 that is the vendor/magento/module-indexer/Setup/Recurring.php class where Magento_Indexer module inspects for the newly defined indexers and appends them into the indexer_state table.

Recurring data event

Your module’s recurring data event class is executed by Magento post each schema installation or upgrade stage. This class then makes the last revisions to the database store after the data has been installed or updated.

For example,
Setup/RecurringData.php

<?php

namespace Aureatelabs\Test\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class RecurringData implements InstallDataInterface
{
	public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
	{
		// Recurring data event logic
	}
}

The only example in current Magento 2 is vendor/magento/module-indexer/Setup/RecurringData.php class.

Thanks

Grow your online business like 4,969 subscribers

    * This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
    envelope

    Thank You!

    We are reviewing your submission, and will be in touch shortly.