Scheduling cron job on Linux
First you need to create bash or shell script for the job that you want to perform and then to schedule that job in a recurring manner we will be using cron tab to schedule it based on our requirement to run it whether hourly, daily, weekly or monthly
1. Sample script for deleting the files that are 5 days old so that i can keep only 5 days backup and later data will be deleted.
# vi deletebackup.sh
#!/bin/bash
find /opt/jirabackup/export/20* -type f -mtime +5 -exec rm -rf {} \;
2. Edit the crontab to schedule this delete backup job to run every day at 3AM
# crontab -e
00 03 * * * /root/deletejirabackup.sh >> /tmp/deletejirabackup.sh.log 2>&1
3. To edit the crontab for another user, use the following command:
# crontab -u acg_user1 -e
4. Cron job time format to refer and based on this format we need create the cron job to perform our regular task
# tail /etc/crontab # Example of job definition: # .---------------- 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 # | | | | | # * * * * * user-name command to be executed
5. To list the scheduled cron job use the following command
# crontab -l
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments