Skip to content

Latest commit

 

History

History
executable file
·
106 lines (74 loc) · 1.97 KB

File metadata and controls

executable file
·
106 lines (74 loc) · 1.97 KB

Troubleshooting

NOTE: echo won't work because cron runs in its own shell.

Possible problems:

  1. Not using absolute paths
* * * * * /bin/echo "cron works" >> /tmp/file
  1. Not using a PATH variable
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * echo "cron works" >> /tmp/file
  1. Not escaping characters
# mysqldump --databases db > /home/user/backup-$(date +%Y%m%d-%H%M%S)
mysqldump --databases db > /home/user/backup-$(date +\%Y\%m\%d-\%H\%M\%S)
  1. Not leaving an empty new line in the end

Cron

Each user has his own crontab i.e. cron table i.e table of scheduled processes.

List

# list of tasks for current user
crontab -l

# list of tasks for specific user
crontab –u username –l

Edit

# edit crontabs for current user
crontab -e

# edit tasks for specific user
crontab –u username –e

# edit tasks for root user
sudo crontab -e

Jobs

# list of crontabs
sudo less /var/spool/cron/crontabs

# root systemwide crontab
sudo less /etc/crontab

Format

.---------------- 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
|  |  |  |  |
*  *  *  *  * command to be executed

*     all
,     list of values
x-y   range of values
/x    recurring i.e. every x times

NOTE: The timing doesn't overlap, meaning it happens in both cases, not a combination of the two.

# Every minute
* * * * *

# Every hour
0 * * * *

# Every 2 hours
0 */2 * * *

# Every day @ 23:59
59 23 * * *

# Sunday @ 23:59
59 23 * * 0

# At every 20th minute past hour 22 on day-of-month 1 and 15
*/20 22 1,15 * *

# At 10:15 on every 2nd day-of-month from 1 through 10 and on Friday
15 10 1-10/2 * 5