Friday, October 18, 2019

Cron Job and Shell script - remove and move old files from UNIX or AIX box automatically

Sometimes Integrations need to store files in UNIX/AIX box. These archive files is storing each day and consuming the UNIX space and we need to manually delete the files to recover the space. Following the steps we can automatically delete the files after certain specified interval.

What is CRON:
The software utility cron is a time-based job scheduler in Unix-like computer operating systems. Users that set up and maintain software environments use cron to schedule jobs to run periodically at fixed times, dates, or intervals.

Implementation steps:

Step1: Create .sh files which will store all the remove and move commands
Move files older than 1 day:
find /soashare/archieve -mtime +1 -type f -exec mv "{}" /soashare/old_archive/ \;
echo "Moved Old Files From hrms archieve to Old_archive: $(date)" >> /soashare/script/logs/Schudule_logs.txt
Remove files older that 2 days:
find /soashare/old_archive -mtime +2 -exec rm {} \;
echo "Removed Old Files From Old_archive: $(date)" >> /soashare/script/logs/Schudule_logs.txt
Note: don't delete .sh files from /soashare/script folder.

Step2: Jobs stored in cron.txt file
 5 0 * * * /soashare/script/MoveOldFileshrms.sh >> /soashare/script/logs/Schudule_logs.txt
 5 0 * * * /soashare/script/RemoveOldFilesOld_archive.sh >> /soashare/script/logs/Schudule_logs.txt

Field Allowed values
—– ————–
minute 0-59
hour 0-23
day of month 0-31
month 0-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

# run five minutes after midnight, every day
5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month
15 14 1 * *     $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun     echo "run at 5 after 4 every sunday"

Step3: Command to add to crontab.
crontab cron.txt
or
crontab -e

You can now see the logs that the files are being moved and removed /soashare/script/logs/Schudule_logs.txt


No comments:

Post a Comment

Featured Post

OIC - Restrictions on Using Stage File Action Operations with the File/Attachment Features of the Connectivity Agent

Restrictions on Using Stage File Action Operations with the File/Attachment Features of the Connectivity Agent When configuring the stage fi...