cron tips

Cron tips

crontab format

Here’s a quick comment that can be added to users crontabs as a reminder to the crontab format:

# m h dom mon dow command

There are various ways to schedule a command to run, such as every 5 minutes, that would be considered acceptable format by some linux distributions, and not accepted by Solaris.

This kb article was intended to be kept simple as a reminder to what order the fields go, something that can be found easily by reading the man page.

[root@linux01 ~]# crontab -l
# m h dom mon dow command
#
# ssh blackhole to prevent brute force attacks
* * * * * /root/scripts/ssh-blackhole.bsh >/dev/null 2>&1
#* * * * * /root/scripts/ssh-blackhole2.bsh &>/tmp/ssh-blackhole.troubleshoot.log
# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed (full path name to command must be used)

Disable cron based mail alerts

The easiest way to disable cron based email alerts is to append the following to the specified command within the users crontab:

>/dev/null 2>&1

Example:

# m h dom mon dow command
59 23 * * * /root/scripts/hotfixes/vzagentdb.sh >/dev/null 2>&1

crond sysconfig

CRONDARGS='-m off' in /etc/sysconfig/crond may also be specified to turn off mail completely. Other postings have also indicated setting the MAILTO="" option in the users crontab also works.

Note that I’ve seen cases where the qmail queue was so high that even after implementing these measures, it seemed to not work, but that was because the qmail queue was draining. Once it completely drained, the madness stopped. I once found a customer was running a cronjob to check the status of httpd every waking moment of uptime. The crontab did not output to /dev/null, and therefore emails were being sent each and everytime the crontjob ran. This script was named “keep-apache-alive” and was subjected as such in the emails, so I looked to see how many of these there were in the users /var/qmail/mailnames/domain.com/info/Maildir/new. There were a LOT, so I blew them away and fixed cron to output to /dev/null, preventing the cronjob from sending emails.

-bash-3.1# find . -type f -exec cat {} \; | grep "keep-apache-alive" | wc -l
-bash-3.1# find . -type f -exec grep -q "keep-apache-alive" {} \; -exec rm {} \; -exec echo removed {} \;
-bash-3.1# find . -type f -exec cat {} \; | grep Subject | more
Share