Script - Remove old log file
I have a script which runs every 10 min. and updates a log file named /home/jack/logsfile.
Code:
.... I am not getting a clue that how to do it? If I add a Code:
rm /home/jack/logsfile |
These the script im running everyday once to backup my log
#!/bin/bash #Script -Purpose:Backup script # Exits with zero if no error. # Step -1 Function to archieve file using date and time date=`/bin/date "+%Y.%m.%d.%H.%M.%S"` # Step -2 Function to create folder on /root/logs with Timestamp mkdir -p /root/logs/$date # Step -3 Function to find & move logs from /data to /root/logs find /data/ -type f -iname *.logs -mtime +30 -print | xargs -I {} mv {} /root/logs/$date #Step -4 Function to compress the log Backup Content tar -cvzf /root/log/$date /root/logarchive/$date.tar.gz #Step -5 Function to remove the Log rm -rf /root/log/* #Step -4 Function to Print the logs Backup status echo "$(date) log Backuped successfully ">>/root/logs/logbackup-status-$date.log #Step -5 Function to call the Mail trigger to user mailid= bkupxx@gmail.com #Step -5 Function to trigger mail after log backup status mail -s '$(date) Logs Moved Successfully $(hostname) - Successful' $mailid |
You can just redirect the black hole to the log file. It won't delete the file but will just clear the contents of the file.
Code:
cat /dev/null > filename.log |
Code:
#!/usr/bin/perl -w |
Why not use the built-in tool 'logrotate' ?
http://linux.die.net/man/8/logrotate |
Used a simple workaround, side-by-side created one more script & added it to cron, which moves such log file (although I have more such scripts) once in a day at some point of time.
Code:
mv /home/jack/archive_logs/logfile /home/jack/archived_logs/logfile.$(date +%Y%m%d) |
use the find command paired with -mtime:
ex: Code:
find /path/to/logfile.log -mtime +1 -exec rm '{}' \; Code:
### Setting up variables. |
hi but this is sh file.
which means once i run it it wont quit even it done the job? or should i cron this? |
At top of script you could:
Code:
tail -n 23 /home/jack/logsfile > tmpfile && mv tmpfile /home/jack/logsfile |
All times are GMT -5. The time now is 03:22 AM. |