7. Crontab

$ crontab -e
$ crontab -l
$ sudo crontab -e

7.1. Przykładowy crontab

5 4 * * *       /bin/echo 'five past four a.m.'
*/10 * * * *    /bin/echo 'every ten minutes'
5-10 4 * * *    /bin/echo 'every minute from 5-10 past four a.m.'
* 4 * * *       /bin/echo 'every minute at 4 a.m.'
0 14 * * *      /bin/echo 'at 2 p.m.'
0 0 1 * *       /bin/echo 'at midnight of first day of month'
0 0 1 JAN *     /bin/echo 'at midnight of first day of January'
0 0 1 1 *       /bin/echo 'at midnight of first day of January'
0 0 * * SAT,SUN /bin/echo 'at midnight on weekends'
0 0 * * 0,6     /bin/echo 'at midnight on weekends'

@daily          /bin/echo 'at midnight'
@weekly         /bin/echo 'at midnight on Sunday'

45 04 * * * /usr/bin/updatedb
45 04 * * * /usr/sbin/chkrootkit && /usr/bin/updatedb
00 06 * * * env DISPLAY=:0.0 gui_appname
00 01 * * * ubuntu /home/ubuntu/script.sh

7.2. Editing crontab

export EDITOR=/usr/bin/vim

7.2.1. Variables

PATH=/usr/sbin:/usr/bin:/sbin:/bin

7.2.2. Special characters

  • * any value

  • , value list separator

  • - range of values

  • / step values

7.2.3. Crontab formatting

  • minute: 0-60

  • hour: 0-23

  • day of month: 0-31

  • month: JAN-DEC / 0-12

  • day of week: SUN-SAT / 0-7 (Sunday = 0 or 7)

7.2.4. Short notation

Table 7.1. Short notation

Notation

Meaning

@yearly

Run once a year, 0 0 1 1 *

@annually

Same as @yearly

@monthly

Run once a month 0 0 1 * *

@weekly

Run once a week 0 0 * * 0

@daily

Run once a day 0 0 * * *

@midnight

Same as @daily

@hourly

Run once an hour 0 * * * *

@reboot

Run once, at startup

7.3. Allowing/Denying User-Level Cron

  • /etc/cron.allow

  • /etc/cron.deny

7.4. Files and Directories

  • /etc/crontab

  • /var/spool/crontab/

  • /etc/cron.d/

  • /etc/cron.daily/

  • /etc/cron.hourly/

  • /etc/cron.weekly/

  • /etc/cron.monthly/

7.5. Other

  • z jakiego użytkownika są uruchamiane

  • przekierowanie outputu stdout i stderr

  • dostawanie maili