Crontab

From Andreida
Revision as of 16:58, 26 February 2025 by Andreas (talk | contribs) (→‎schedules)

basics

The commands you put into your crontab will be executed unless you start a line with "#". If the command has any output and you have set the variable MAILTO before that line, the output of the command will be sent to you.

You can have as many MAILTO lines as you wish, the last before your cronjob line will be used. So every cronjob can send mail to another mail address or - if MAILTO has been set to nothing (MAILTO=) - no mail will be sent at all.

When cron is being executed, often no PATH has been set. So you can often make it work with something like

33 16 3 * * . /etc/profile && php /root/scripts/myScript-0815.php

If you don't want or can suppress the output of a cronjob, but don't want the everyday messages, redirect the normal output to the logs.

55 5 * * * . /etc/profile && apt-get -q update | logger -t apt-get && apt-get -q -y upgrade | logger -t apt-get

The "-t apt-get" creates log entries with the given tag.

mail / FROM

bsd-mailx

The easiest way to check the current "FROM" of your cronjob is

crontab -e 
MAILTO=root
* * * * * date

Now you will get a new mail every minute and can see the current FROM. Some changes need a restart of the system to be active or calls of other programs.

First thing you should do is

ls -l /etc/alternatives/mail

If it is not bsd-mailx, then you COULD try

apt-get install bsd-mailx
ls -l /etc/alternatives/mail

If the new link is now bsd-mailx, do

shutdown -r now

and wait for the next mail from the cronjobs. If it looks ok, ok. :-)

Files to check

Multiple files can change the behavior of send mails. Check the following files, if they exist. Some do NOT have to exist!

/etc/hosts

cat /etc/hosts

It should look like this

127.0.0.1       localhost localhost.localdomain
::1     localhost localhost.localdomain
xxx.xxx.xxx.xxx servername.SLD.TLD servername
xxxx:xxxx:xx::xxxx:xxxx servername.SLD.TLD servername

/etc/hostname

cat /etc/hostname

It should be something like

servername

/etc/mailname

cat /etc/mailname

It should be something like

servername.SLD.TLD

/etc/aliases

cat /etc/aliases

It COULD be something like

www: root
ftp: root
root: yourRealMail_or_your-normal-user-on-this-server

MAILFROM

You can try

MAILFROM=cron@your.domain.com

to get a meaningful "from" in sent mails. But if you need to do this, then something is wrong in your configuration. BUT, it works - normally - and will save you hours of searching and testing if you just want it to "be" that way.

And yeah, I was there. Two servers working "correctly", a third one needed MAILFROM to send mails with a meaningful FROM address. Perhaps wrong exim configuration? Who knows....  :-(

commands

  • list the cron jobs of the current user
crontab -l 
  • edit/create cron jobs
crontab -e

If you do not get vim as editor for your cronjobs, you can set it with the environment variable "VISUAL". Find out what is the path to vim on your system:

which vim

Then put this into your ~/.bashrc:

export VISUAL=/usr/bin/vim

then reload/source the .bashrc with

. ~/.bashrc

Do not forget the "." in front.

schedules

The following will clear the Apache error log at one minute past midnight (00:01 of every day of the month, of every day of the week). This is only an example, do not use it for real, you do NOT want to clear the log manually. What you want is an automatic rotation.

1 0 * * *  echo -n "" > /www/apache/logs/error_log

The following will run the script /home/user/test.pl every 5 minutes.

*/5 * * * *  /home/user/test.pl


SHELL=/bin/bash
PATH=/sbin:/bin/:/usr/sbin/:/usr/bin
MAILTO=root
HOME=/
.---------------- minute (0 - 59) 
|  .------------- hour (0 - 23)
|  |  .---------- day of month (1 - 31)
|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ... 
|  |  |  |  .---- day of week (0 - 7) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat 
|  |  |  |  |
*  *  *  *  *  command to be executed
  • for numerics:
    • lists 2,4,7
    • ranges 2-5
    • combination of list and range 2-4,6,8-11
  • if "day of month" and "day of week" are both not "*", then they both are used, they do not restrict each other anymore
  • steps after an asterix: */2 every 2 whatever