Sponsored Content
Top Forums Shell Programming and Scripting zipping older files with cron jobs Post 302698069 by rajkumarin on Saturday 8th of September 2012 01:27:35 AM
Old 09-08-2012
Code:
###Go to the folder which contains log files
cd /root/Desktop/Scripts/logs
ls -lt > /root/Desktop/Scripts/logdate.txt


###Filtering the files with date
cat /root/Desktop/Scripts/logdate.txt | cut -d" " -f8  | sed  "/^$/d" > /root/Desktop/Scripts/date.txt


###Filtering the files with filename it will be usefull in the end
cat /root/Desktop/Scripts/logdate.txt | cut -d" " -f10  | sed  "/^$/d" > /root/Desktop/Scripts/filename.txt


a=`cat /root/Desktop/Scripts/date.txt`
b=`date | cut -d" " -f4`
for i in $a
do
##Comparing todays date with the files date and if it is lesserthan equal to 3 do the condition
if [ $(echo $b-$i | bc) -le 3  ]
then
logfile=`cat /root/Desktop/Scripts/filename.txt | sed -n '1p'`
##file is last modified with in last 3 days then zip the file
zip -9 ../$logfile.zip /root/Desktop/Scripts/logs/$logfile 
sed -i '1d' /root/Desktop/Scripts/filename.txt
else
##if the file is modified before 4 days or more delete it
logfile=`cat ../filename.txt | sed -n '1p'`
rm -rf $logfile
sed -i '1d' /root/Desktop/Scripts/filename.txt
fi
done
echo "Log file script runs successfully" | mail -s "script runs successfully" xyz@gmail.com

Cronjob
Code:
0 1 * * * /bin/sh /root/Desktop/Scripts/script.sh


Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.

Last edited by Franklin52; 09-09-2012 at 11:18 AM..
This User Gave Thanks to rajkumarin For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

cron jobs

hi folks, I need an explanation on cron jobs with an example (4 Replies)
Discussion started by: vijaysabari
4 Replies

2. UNIX for Dummies Questions & Answers

cron jobs

I was wondering if itīs possible to cron job not to run on a certian day and time. Iīve got a job that runs everyday at 08:00 but would like it not to run on the 20:th between 08:00 and 10:00 Anyone know if this is possible, and if. How do i do it? regards... dOzY (3 Replies)
Discussion started by: dozy
3 Replies

3. UNIX for Dummies Questions & Answers

Cron Jobs

Where can someone find info on Cron Jobs? Very new to UNIX and the PC I inherited looks to have several of them. Looks like they are some kind of background program that runs automatically at specified times. Would like to delete some of them and know more about them. (6 Replies)
Discussion started by: dereckbc
6 Replies

4. Solaris

Cron Jobs

whats up, Have some questions about cron jobs, I am fairly new to the unix os. My cronjob sends all output to my mail, does anyone know how to redirect output to come out on the screen??? Thanks...... (6 Replies)
Discussion started by: lewisoco
6 Replies

5. Linux

cron jobs

I created a php script. I'd like to have it run as a cron job every day at 10:30am. So i added 30 10 * * * /script.php to the cron file Is it possible to run the script on the command line to test it out ? (1 Reply)
Discussion started by: dannyd
1 Replies

6. Shell Programming and Scripting

Zipping files older than one month

I have to zip all files older than a month within a directory. I have to archive them using the file extension I have .dat, .csv ,.cnt files within the directory. I used the following command It doesnt work find /path/*.dat -mtime +30 This command doesnot display .dat files older than a... (2 Replies)
Discussion started by: ramky79
2 Replies

7. UNIX for Advanced & Expert Users

cron jobs...

I need to start a job every friday night at 8:00 P.M , it runs all the day on Sat and Sun....can somebody tell me how to do this...I understand crontab...but haven't used it.........can u write some steps.....how to create a file and call.....I honestly dont know? Plz help.Thanks (2 Replies)
Discussion started by: RubinPat
2 Replies

8. UNIX for Dummies Questions & Answers

cron jobs

Hi, We have a group of hosts using which the cron jobs are submitted... Few days ago i had submitted a cron job in of these hosts, but unfortunately forgot the host name :( Can anyone please help me out in finding this host name from which the cron s submitting the job, i dont want the... (2 Replies)
Discussion started by: bhavanisree
2 Replies

9. Shell Programming and Scripting

Help with cron jobs

Hi Frenz, How do we get a cron job running in background to foreground ? (3 Replies)
Discussion started by: mkalase
3 Replies

10. Solaris

Cron jobs and at jobs

There are two jobs in Solaris , Cron and at jobs.. I know how to disable or enable cron jobs. How can I enable at jobs and disable it. Kindly help. Rj (2 Replies)
Discussion started by: jegaraman
2 Replies
All times are GMT -4. The time now is 11:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy