Tuesday, December 13, 2016

cron automation

Use cron to setup automated log file removal or other periodic tasks in linux environment is very useful. Here are the steps to automate that.

1. Run the following command to export the current crontab:
 
crontab -u john -l > john-cron-backup.txt

2. Edit the exported john-cron-backup.txt file to your need.

3. Run the following command to restore from the edited file to make it taking effects:
    
crontab -u john john-cron-backup.txt
 
Here is an example backup file: 

MAILTO=""
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin

# m h  dom mon dow   command
0 1 * * 1 rm -r -f /var/log/**/*.gz
0 1 * * 1 rm -r -f /var/log/**/*.log.1
0 1 * * 1 rm -r -f /var/log/*.gz
 
    
 

No comments:

Post a Comment