Cronjob fails to connect to mysql

Well I finally found my solution: I changed the parallel job execution to a sequential one. I noticed that if I spaced the cronjobs preventing them from running at the exact same time the MySQL server wouldn’t crash so often but then I figured it’d be easier to simply have them run one after the other instead of trying to calculate the best cronjob distribution.

So what I did was create a single job that would spawn all of the tasks (run-jobs.sh):

#!/bin/bash

export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

php /var/www/html/bin/console mautic:reports:scheduler > /var/log/cron.pipe 2>&1
php /var/www/html/bin/console mautic:messages:send > /var/log/cron.pipe 2>&1
php /var/www/html/bin/console mautic:emails:send > /var/log/cron.pipe 2>&1
php /var/www/html/bin/console mautic:campaigns:trigger > /var/log/cron.pipe 2>&1
php /var/www/html/bin/console mautic:email:fetch > /var/log/cron.pipe 2>&1
php /var/www/html/bin/console mautic:social:monitoring > /var/log/cron.pipe 2>&1
php /var/www/html/bin/console mautic:broadcasts:send > /var/log/cron.pipe 2>&1
php /var/www/html/bin/console mautic:webhooks:process > /var/log/cron.pipe 2>&1
php /var/www/html/bin/console mautic:segments:update > /var/log/cron.pipe 2>&1
php /var/www/html/bin/console mautic:campaigns:rebuild > /var/log/cron.pipe 2>&1

And then simplified the cron file to:

SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

*/7 * * * * 	www-data   /var/www/html/run-jobs.sh
* 1 * * *     	www-data   php /var/www/html/bin/console mautic:maintenance:cleanup -n --days-old=365 > /var/log/cron.pipe 2>&1
0 4 15 * *     	www-data   php /var/www/html/bin/console mautic:iplookup:download > /var/log/cron.pipe 2>&1
       
0 5 10 * *     	www-data   php /var/www/html/bin/console mautic:unusedip:delete > /var/log/cron.pipe 2>&1

This way I can have all the jobs at a higher frequency than the original, while also keeping my MySQL server sane :stuck_out_tongue:

I’ll probably need to adjust this if the amount of emails sent grows but I think the general idea will be kept.

Also, running mysql_upgrade on the database server helped get rid of some annoying errors found in /var/log/mysql/error.log.

I hope this helps :slight_smile:

1 Like