It can be achieved by by overriding the crontab.xml file of CustomModule1 in your CustomModule2.
For testing purpose, I have overridden a few crons from Magento_Indexer module in my custom module as given below.
Added following code to etc/module.xml
<?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="Amitshree_Mymodule" setup_version="1.0.0">
<sequence>
<module name="Magento_Indexer"/>
</sequence>
</module>
</config>
And added below code to etc/crontab.xml file
<?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="index">
<job name="indexer_reindex_all_invalid" instance="Magento\Indexer\Cron\ReindexAllInvalid" method="execute">
<schedule>5 * * * *</schedule>
</job>
<job name="indexer_update_all_views" instance="Magento\Indexer\Cron\UpdateMview" method="execute">
<schedule>9 * * * *</schedule>
</job>
<job name="indexer_clean_all_changelogs" instance="Magento\Indexer\Cron\ClearChangelog" method="execute">
<schedule>4 * * * *</schedule>
</job>
</group>
</config>
Question: How to verify it works properly?
Answer: Follow below steps to verify it.
- After making above changes flush your cache.
- run command
php bin/magento cron:run from root of your project
- Connect to your database via sql command
use magento2;
run below sql queries
1. select job_code, scheduled_at from cron_schedule where job_code = 'indexer_reindex_all_invalid';
Output:
+-----------------------------+---------------------+
| job_code | scheduled_at |
+-----------------------------+---------------------+
| indexer_reindex_all_invalid | 2020-02-26 11:05:00 |
+-----------------------------+---------------------+
2. select job_code, scheduled_at from cron_schedule where job_code = 'indexer_update_all_views';
Output:
+--------------------------+---------------------+
| job_code | scheduled_at |
+--------------------------+---------------------+
| indexer_update_all_views | 2020-02-26 11:09:00 |
+--------------------------+---------------------+
3. select job_code, scheduled_at from cron_schedule where job_code = 'indexer_clean_all_changelogs';
Output:
+------------------------------+---------------------+
| job_code | scheduled_at |
+------------------------------+---------------------+
| indexer_clean_all_changelogs | 2020-02-26 11:04:00 |
+------------------------------+---------------------+