When Magento’s cron line is added with sudo, it ends up in root’s crontab and Magento runs as root every minute. Every cache file, log and generated class it creates is owned by root, and the web server can no longer change them.

What it looks like

  • Random errors about files that cannot be written in var/ or generated/.
  • Deployments that stop halfway with “Permission denied”.
  • Static content that will not redeploy.

It’s also a security risk, because any code Magento runs, including extensions, runs with full system privileges.

Check it

sudo crontab -u root -l | grep magento
sudo find var/ generated/ -user root | head

Move it to the web server user

  1. Remove the Magento line from root’s crontab: sudo crontab -u root -e.
  2. Add it to the web user’s crontab, usually www-data: sudo crontab -u www-data -e
    * * * * * /usr/bin/php /var/www/magento/bin/magento cron:run
  3. Give back the files root already created:
    sudo chown -R www-data:www-data var/ generated/ pub/static/ pub/media/

Prevent it

Run bin/magento cron:install and all deploy commands as the web server user, never with plain sudo. Use sudo -u www-data instead.

Catch it automatically

MageMonitor checks for this every few minutes as part of its Cron Health checks, and alerts you by email, Slack or webhook the moment it appears, with the exact commands to fix it. See MageMonitor for Magento 2.

Related fixes