# system wide crontab
nano /etc/crontab
 
# edit user crontab file, or create one if it doesn't already exist.
crontab -e
 
# display the last time you edited your crontab file. (this option is only available on a few systems.)
crontab -v

environment

  • invokes the command from the user's HOME directory with the shell, (/usr/bin/sh).
  • defines default environment for every shell:
HOME    # user's-home-directory
LOGNAME # user's-login-id
PATH    # /usr/bin:/usr/sbin:.
SHELL   # /usr/bin/sh

you can overide them:

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

logging

disable email

cmd > /dev/null 2>&1

log to different files by month/year

cmd > /var/log/ID_job_`date +"%F"`.log
 
0 0 1 * * /home/User/script.sh > /home/User/cronlog/`date +\%Y-\%m-\%d-\%H:\%M:\%S`-cron.log 2>&1 ; mailx -s "CronJob run" test@gmail.com

script output to mail and log file

* 2 * * * /home/user/command | tee -a /var/log/app/app.log

cron log trace per vedere se la schedulazione è partita

sudo less /var/log/syslog
sudo grep CRON /var/log/syslog
 
# filtrata
sudo grep -i CRON /var/log/syslog | grep $script_file
nano /etc/rsyslog.conf
uncommenting cron.* /var/log/cron.log
/etc/init.d/rsyslog restart

users

run as different user (www-data)

*/5 * * * * www-data php /var/www/public/voto_m/artisan top >/dev/null 2>&1

or user a different user crontab(becomes scattered to manage)

sudo su -c "crontab -e" www-data

ricorda di settare il mail alias per ricevere le notifiche via email

examples

  • generator
  • Repeat pattern like /2 or /10 is not supported by all distros
  • The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, both will get executed
*  *  *  *  *  command to be executed
-  -  -  -  -
|  |  |  |  |
|  |  |  |  +---- day of week(0-6) (sunday=0)
|  |  |  +------ month (1-12)
|  |  +-------- day of month (1-31)
|  +---------- hour (0-23)
+------------ min (0-59)
30   18   *  *  *  # every day day at 18:30
0    */2  *  *  *  # evry two howrs
1,31 * * * *   # evry 30 minutes
*/30 * * * *   # evry 30 minutes
*/5  * * * *   # evry 5 minutes
*    * * * *   # evry minute
* 9-17 * * * # from 9:00 to 17:00
* * * * 1-5  # non week-end days
1,31  9-17  * * 1-5  # evry 30 minutes, only working hours, non week-end days

compound:

30   0     1    1,6,12 *   # 00:30 hrs  on 1st of jan, june & dec.
0    20    *    10     1-5 # 8.00 pm every weekday (mon-fri) only in oct.
0    0     1,10,15     *   *  # midnight on 1st ,10th & 15th of month
5,10 0     10    *     1   # at 12.05,12.10 every monday & on 10th every month

perform an extra test before running the script

# test if current is less than 18:15
0,30 9-18 * * * [ $(date +\%H\%M) -le 1815 ] && cmd

short syntax

@reboot  : Run once after reboot.
@yearly  : equivalent to "0 0 1 1 *".
@annually: equivalent to "0 0 1 1 *".
@monthly : equivalent to "0 0 1 * *".
@weekly  : equivalent to "0 0 * * 0".
@daily   : equivalent to "0 0 * * *".
@hourly  : equivalent to "0 * * * *".

troubleshooting

  • riavviare il servizio /etc/init.d/cron stop per vedere se il file crontab contiene errori di sintassi

eliminare dati e logs vecchi:

sudo find /var/log/apache2/ -maxdepth 1 -type f -mtime +180 -delete