Skip to content

cron

cron requirements

Files in /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly:

  • be executable
  • be owned by root
  • not be writable by group or other
  • if symlinks, point to files owned by root
  • file names must be entirely made up of letters, digits and can only contain the special signs underscores ('_') and hyphens ('-').

Any file that does not conform to these requirements will not be executed by run-parts. For example, any file containing dots will be ignored.

File in /etc/cron.d:

  • must provide the username to run the task
    0,15,30,45 * * * * root /backup.sh
    
    getent passwd root
    getent shadow root
    
  • owned by root
  • must not be group- or other-writable
    0644
    
  • do not need to be executable (they are configuration files, just like /etc/crontab)
  • file names must be entirely made up of letters, digits and can only contain the special signs underscores ('_') and hyphens ('-').
  • line break after the last entry

The /etc/cron.deny file includes the list of users not allowed to use crontab, without this file, only users listed in /etc/cron.allow can use it.

Add background task for user www-data

crontab -u www-data -e
*/10 * * * * /usr/bin/php /var/www/moodle/admin/cli/cron.php  >/dev/null

Cron mail FROM address

When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists)

If MAILFROM is defined (and non-empty), it will be used as the envelope sender address, otherwise, ''root'' will be used.

Cron in Ubuntu 20.04 and below does not support MAILFROM= in the crontab format.

https://serverfault.com/a/437319

To confirm that cron will be able to send mail

apt install bsd-mailx
yum install mailx
echo "hello from `uptime` `who -q`" | mail -s "`testsubject" root
cat /var/log/mail.log
cat /var/log/mail.err
cat /var/log/syslog

cat /var/mail/root
cat /var/mail.log

Alternatively add a cron job:

/etc/cron.d/testmail

*/5 * * * * root echo "testcron"

Wait for it to run (journalctl -ef or watch date), then cat /var/mail/root.

Another test send

echo foo | /usr/sbin/sendmail -f root root

https://bobcares.com/blog/crontab-not-sending-email/ https://wiki.archlinux.org/title/cron

cron MAILTO

For cron, the default value of MAILTO is root. We can change the root value of the MAILTO variable via the /etc/crontab config file to "" (blank). Example:

MAILTO=""

This disables cron daemon’s emails.

cron arguments. Send output to syslog instead of email

If you disable cron emails completely and something goes wrong, you will lose the output. You can get around this by setting CRONDARGS string. For example:

CRONDARGS= -s -m off

-s = forwards the output to system log.

-m off = disables cron emails.

On RHEL/Fedora, you can edit /etc/sysconfig/crond. So that it looks similar to:

# Settings for the CRON daemon.
# CRONDARGS= : any extra command-line startup arguments for crond
CRONDARGS= -s -m off

For Debian/Ubuntu, you can edit using:

systemctl edit --full cron.service

https://haydenjames.io/disable-cron-emails/

crontab

The cron daemon parses a configuration file known as crontab. Each user on the system can maintain a separate crontab file to schedule commands individually. The root user's crontab is used to schedule system-wide tasks (though users may opt to use /etc/crontab or the /etc/cron.d directory, depending on which cron implementation they choose).

The crontab files themselves are usually stored as /var/spool/cron/username. For example, root's crontab is found at /var/spool/cron/root


A crontab file contains instructions to the cron daemon of the general form: "run this command, at this time on this date". Jobs specified in /etc/crontab, which may contain system-level jobs that can be executed as any user, should also specify an user to execute as. Each user has their own crontab, and commands in any given crontab will be executed as the user who owns the crontab.

crontab entries are examined once every minute.

Blank lines, leading spaces and tabs, and lines whose first non-space character is a pound-sign (#) -comments- are ignored. Comments are not allowed on the same line as cron commands, since they will be taken to be part of the command. Similarly, comments are not allowed on the same line as environment variable settings.

An active line in a crontab will be either an environment setting or a cron command.

https://wiki.novaordis.com/index.php/Cron#cron_Jobs.27_Output

Cron job every 5 minutes

/etc/cron.d/testjob

*/5 * * * * root echo "testcron"