Tuesday, February 4, 2014

Configure crontab (UNIX/Linux)


If you are about to modify a crontab, it's better to keep a backup:
In command line:
> crontab -l > /home/usr/crontab_[Modification date : YYYY_MM_DD_hh_mm]_bk 

To modify the crontab, you first need to create a copy of the actual configuration:
crontab -l > /home/usr/crontab_current.txt

# .------------------- 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 sun, mon, tue, wed, thu, fri, sat
# |   |   |   |   |
# *   *   *   *   *   command to be executed
# Operators : * = all
#             , = and
#             - = to
#
# Everyday of the week: 3:40, 7:40, 12:40, 16:40 and 22:40
40 3,7,12,16,22 * * * /application/script/run_refresh.sh param1 param2 > /dev/null


When the modification is over,
> crontab /home/usr/crontab_current.txt

If you are modifying the file in Windows, don't forget to convert the EOL:
> dos2unix /home/usr/crontab_current.txt /home/usr/crontab_current.txt


If you want to periodicaly delete log files, you can add this to the crontab:
> find /opt/logs -name "*.log" -type f -mtime +1 -delete

No comments:

Post a Comment