Running Drupal with Cron Without the Emails

To start off: let's define cron. Cron is a daemon in UNIX like operating systems, including Linux, that lets you schedule tasks. It's what lets you do things like run a virus scan every Sunday morning, or run that fun custom backup script you wrote every night after you've hit the sack. So Drupal is controlled via a script, site/cron.php. Hitting that script causes Drupal to run its cron jobs which are defined throughout the CMS' configuration. In short: this is how things get done. Drupal's cron jobs send out emails, run submitted content through spam filters, update RSS feeds, and many other activities you expect from a dynamic site. When I moved MI-80 to the new server I ran into a small issue. On my old host I relied on the crutch of cPanel, and when SUSE lacked a GUI cron tool I had to brush off my rusty cron skills. Cron is hardly difficult, but if you barely use it it's easy to forget the syntax, so I had to re-learn it basically. Any who so I had a simple goal: get cron running so Drupal would run its scheduled tasks once an hour. Task 1: Writing the Script Writing the script was easy. The miracle of Linux is you can basically toss any command line strings into a file, mark it executable, and run it. I nailed this on the first try (functionality wise) as below: su cd ~ nano -w drupal_cron.sh wget http://www.mi80.com/cron.php && rm /root/*cron*.php (enter this into the script and save, duh!) chmod +x drupal_cron.sh So nice and simple: grab the cron.php file causing Drupal to run its tasks, then delete them. Task 2: Making it Run Hourly This part was again pretty simple. I simply hit Wikipedia's article on Cron for a reminder: Wiki's Summarizing of Cron: # (Use to post in the top of your crontab) # ------------- minute (0 - 59) # | ----------- hour (0 - 23) # | | --------- day of month (1 - 31) # | | | ------- month (1 - 12) # | | | | ----- day of week (0 - 6) (Sunday=0) # | | | | | # * * * * * command to be executed So with that I run crontab -e (the command to edit my cron jobs) and enter: 0 * * * * /root/drupal_cron.sh It works! The Problem: Root Emails Now I run into a problem: I'm emailed the output of each successful cron job, every...single...hour. That gets annoying, so I started looking into making it so cron wouldn't email me. First Attempt to Fix: > /dev/null My first try came from a buddies recommendation. Adding > /dev/null to the end of the script would move the output to the null device, giving the cron job nothing to email me. But after I did this, the emails kept coming. So 0 * * * * /root/drupal_cron.sh > /dev/null was a no go. Second Attempt to Fix: /etc/crontab So I spent a few minutes on Google. I started out with things like “stop cron emailing” and got some hits, mostly of people posting on forums asking for help. Initially I thought I saw promise in /etc/crontab, after all it contains a line: MAILTO=root. Well I commented that out and it didn't fix a damned thing. Third Attempt to Fix: 2>&1 So I spent a bit more time on Google, and it hit me after reading a few posts: you have to deal with both standard output and error output: it's a programming thing. But the point is I need both of them redirected to /dev/null. The end result was stupid simple: modify the script to appear as follows: 0 * * * * /root/drupal_cron.sh > /dev/null 2>&1 Now I've got the site updating and running maintenance hourly, and most importantly I'm not being emailed hourly letting me know it's doing its job. Over all it was a simple, fun, and educational experience. That's why I love open source.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Duh, I coulda told you that.

Duh, I coulda told you that. You never said you needed to redirect error output.

I hate you. I really do.

I hate you. I really do.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.