Magento2 Cron job not running
-
17-12-2020 - |
题
I have made all the admin settings based on previous post from magento stack exchange. I have created a custom module to send emails and I want to schedule the email based on cron job. I want to know what is missing. If I run manually the cron job runs successfully and email is sent out. How do I make cron job run in the background so email gets sent out in a scheduled time everyday.
My Admin config settings:
My custom code crontab.xml
is as below:
<?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="Daily_report" instance="Module\Modulename\Cron\sendEmail" method="execute">
<schedule>*/1 * * * *</schedule>
</job>
</group>
</config>
Here is the crontab set-up:
*/1 * * * * www-data php /var/www/html/magento2/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/html/magento2/var/log/magento.cron.log
*/1 * * * * www-data php /var/www/html/magento2/update/cron.php >> /var/www/html/magento2/var/log/update.cron.log
*/1 * * * * www-data php /var/www/html/magento2/bin/magento setup:cron:run >> /var/www/html/magento2/var/log/setup.cron.log
UPDATE:
I've updated crontab -e as below:
*/1 * * * * /usr/bin/php /var/www/magento2/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/magento2/var/log/magento.cron.log
*/1 * * * * /usr/bin/php /var/www/magento2/update/cron.php >> /var/www/magento2/var/log/update.cron.log
*/1 * * * * /usr/bin/php /var/www/magento2/bin/magento setup:cron:run >> /var/www/magento2/var/log/setup.cron.log
I can see in /var/log/syslog that the cron is running. Below are the details:
Nov 25 12:52:01 CRON[30803]: (ubuntu) CMD (/usr/bin/php /var/www/magento2/bin/magento setup:cron:run >> /var/www/magento2/var/log/setup.cron.log)
Nov 25 12:52:01 CRON[30800]: (CRON) info (No MTA installed, discarding output)
Nov 25 12:52:01 CRON[30804]: (ubuntu) CMD (/usr/bin/php /var/www/magento2/update/cron.php >> /var/www/magento2/var/log/update.cron.log)
Nov 25 12:52:01 CRON[30805]: (ubuntu) CMD (/usr/bin/php /var/www/magento2/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/mage$
Nov 25 12:52:01 CRON[30801]: (CRON) info (No MTA installed, discarding output)
Nov 25 12:52:02 CRON[30802]: (CRON) info (No MTA installed, discarding output)
However, none of the cron jobs ran. There is nothing in magento log files.
Could you please help me out? I have hosted my test server in AWS Ubuntu server.
解决方案
first find php path using below command
which php
whatever results come you need replace with php into cron setup
Also add below cron using ubuntu user : crontab -e add below crontab
* * * * * php /var/www/html/magento2/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/html/magento2/var/log/magento.cron.log
* * * * * php /var/www/html/magento2/update/cron.php >> /var/www/html/magento2/var/log/update.cron.log
* * * * * php /var/www/html/magento2/bin/magento setup:cron:run >> /var/www/html/magento2/var/log/setup.cron.log
Remove crontab from root user.
其他提示
If you run Magento in a cluster with several instances, you have to make sure to pick a master server that runs cron jobs.
Nethertheless, you can check if the cron was installed correctly with crontab -l
.
If the cron does not show up, run bin/magento cron:install
in your Magento dir. This command is available with Magento 2.2
I do not know much about AWS servers nevertheless everything makes me think that you did not configure any cronjob in you server's crontab.
With regards of AWS, here is an possible explanation of how to set cronjob in general: https://www.cumulations.com/blogs/37/How-to-write-Cron-jobs-on-Amazon-Web-ServicesAWS-EC2-server
And here is how to set a cronjob for Magento: http://devdocs.magento.com/guides/v2.0/config-guide/cli/config-cli-subcommands-cron.html
So far you just configured Magento's inner so-called cronjob. It is not a real cronjob but a managed queue periodically run by the cronjob. So, in short, you should ssh to your server and execute:
crontab -e
And then:
* * * * * <path to php binary> <magento install dir>/bin/magento cron:run | grep -v "Ran jobs by schedule" >> <magento install dir>/var/log/magento.cron.log
* * * * * <path to php binary> <magento install dir>/update/cron.php >> <magento install dir>/var/log/update.cron.log
* * * * * <path to php binary> <magento install dir>/bin/magento setup:cron:run >> <magento install dir>/var/log/setup.cron.log
Cron will then execute magento's scripts which are actually responsible for managing queue and tasks.
for a time period of less than 15 minutes, use the group id index
<?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="Daily_report" instance="Module\Modulename\Cron\sendEmail" method="execute">
<schedule>*/1 * * * *</schedule>
</job>
</group>