Remove Log Files Using Cron Job

Remove log files using cron job

Use wildcard. And just put it in your crontab use the crontab -e option to edit your crontab jobs.

See example:

* * * * *  find  /path/to/*.log -mtime +7 -exec rm -f {} \; 

Just to increment the answer check this nice article on how to work with your crontab ! in Linux .

Deleting old files using cron job in Linux

Just look for *.gz files and delete them.

find /var/log/tomcat8/ -name '*.gz' -mindepth 1 -mtime +1 -delete

Before deleting, just list the files to make sure you are deleting the correct ones.

find /var/log/tomcat8/ -name '*.gz' -mindepth 1 -mtime +1 -print

Empty log files daily using cron task

You can't redirect to more than one file, but you can tee to multiple files.

tee /home/user/dir/log/*.log </dev/null

The redirect from /dev/null also avoids writing an empty line to the beginning of each file, which was another bug in your attempt. (Perhaps specify nullglob to avoid creating a file with the name *.log if the wildcard doesn't match any existing files, though.)

However, a much better solution is probably to use the utility logrotate which is installed out of the box on every Debian (and thus also Ubuntu, Mint, etc) installation. It runs nightly by default, and can be configured by dropping a file in its configuration directory. It lets you compress the previous version of a log file instead of just overwrite, and takes care to preserve ownership and permissions etc.

shell job to compress & delete logs, once in a month

The first problem to solve is the fact that you want a more generic function that can be run any month and produce the correct result. A good tool to get the information you need(Abbreviated month and last two digits of the year) is date.

date -d "last month" +%b

When run on April 1st, 2019 will produce "Mar".

date -d "last month" +%b%y

When run on April 1st, 2019 will produce "Mar19".

Now that we know how to get the information we want, placing the date commands in the tar command will automatically produce what the result you're looking for.

tar -cvzf somezipfile_$(date -d "last month"%b%y).tar.gz somelogfile_**$(date -d "last month" +%b)** --remove-files

The last issue that exists is scheduling, which can be solved using cron. The below statement will run /bin/foobar, on the 5th day of every month when added to your crontab file. (crontab -e to edit your crontab file)

0 0 5 * * /bin/foobar

Combining everything together, you get:

0 0 5 * * /bin/tar -cvzf somezipfile_$(date -d "last month"\%b\%y).tar.gz somelogfile_**$(date -d "last month" +\%b)** --remove-files

Don't forget to escape the %'s in the crontab

how to set crontab every 10 minutes delete content of log file on linux

Open root's crontab for editing in default editor:

sudo crontab -e

Then add this line:

*/10   *    *    *    *    echo '' > /var/log/httpd/access_log

how to remove logs in every 2 hours in Linux Terminal

0 */2 * * * <username> echo "" > /home/test/dev/sample.log

add in /etc/crontab, restart crontab service.

Or maybe it's better to use logrotate.

How to stop auto-generation of log files by cron?

Original throwaway comment now posted as an answer.

The day you stop generating the logs is the day that you need them...

My suggestion would be create a dated daily pair of files (KiranBOT1_YYYYMMDD.err, and KiranBOT1_YYYYMMDD.out), and use logrotate to remove after (say) one week?



Related Topics



Leave a reply



Submit