Drush cron's not so quiet --quiet flag
I use drush to execute cron on my Drupal sites. After upgrading drush from version 5 to version 8, I started receiving empty emails from cron each time drush was executed. Checked my cron entries and everything looked fine. Researched a whole bunch on the Internet. Couldn’t find much of anything about empty emails related to drush so I redirected the output to a log. Sure enough, even with the –quiet option, drush cron still outputs a blank line. Why? I’ve no clue.
I’ve had this happen on two separate servers so I rechecked the docs at drush.org. The recommended drush crontab entry is:
10 * * * * /usr/bin/env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin COLUMNS=72 /usr/local/bin/drush --root=/path/to/your/drupalroot --uri=your.drupalsite.org --quiet cron\r\n
This matches my crontab entries so no problem there. I’ve submitted an issue on github. Until it’s resolved, I’ve opted not to send the output to /dev/null but log it in case there are any errors. Here’s what I did to ensure I still receive mail if there’s actual output. Note this should work out of the box for Debian Jessie. Any other OS, please adjust accordingly.
1. Create log file. Be sure to give ownership to whatever user cron runs the jobs as.
> mkdir /var/log/drush
> touch /var/log/drush/drush-cron.log
> chown -R www-data:adm /var/log/drush
2. Create a file, drush-cron, in /etc/logrotate.d. Insert (again remembering to modify permissions for your cron user):
/var/log/drush/*.log {
daily
rotate 7
compress
delaycompress
notifempty
create 0640 www-data adm
}
3. Create bash script to clean up the log file. I needed to deal with the blank lines in the log file and ensure I’d receive an email if there’s some legitimate output. For this, I created a simple bash script, using sed to remove blank lines then testing for an empty log file. If it’s not empty, it cats the log file and cron mails it to me. Name the script what you’d like. I named mine drush-log-cleanup.sh.
#!/bin/bash
# The drush command for cron, even with the --quiet option
# outputs a single blank line which causes cron to send an
# empty email
# This script removes the blank lines from the log and mails
# any output. There should NOT be any output. If there is,
# it's a problem.
FILE="/var/log/drush/drush-cron.log"
# Remove blank lines
/bin/sed -i '/^\\s*$/d' $FILE
# Test for presence of any messages
if [[ -s $FILE ]] ; then
echo -e "\\n$FILE has data.\\n"
/bin/cat $FILE
fi
4. Modify the cron jobs to write any output to the log file. Append this to each drush cron job entry.
>> /var/log/drush/drush-cron.log
Your cron job entry should now look like this:
10 * * * * /usr/bin/env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin COLUMNS=72 /usr/local/bin/drush --root=/path/to/your/drupalroot --uri=your.drupalsite.org --quiet cron >> /var/log/drush/drush-cron.log
5. Add an entry to cron to execute drush-log-cleanup.sh after your drush cron job. I used a two minute offset. The script removes the blank lines and mails you if there’s any output.