I want to delete all files matching 'a.*' if the used disk space in /tmp exceeds 50%

I need to delete all files starting with 'a.' as soon as capacity of my /tmp exceeds 50% and this command should always run in background. I am new to Unix, so can someone help me.

Answers


There is a lot you can do without costly forks and pipes to parse and match command output. My suggestion is to use a command script like

#!/bin/sh
PATH=/bin:/usr/bin:/sbin:/usr/sbin   # Cron does not export any PATH.
case `df /tmp` in
    (* [5-9]?% *) rm -f /tmp/a.*
esac

This works independently of the column where the percentage used is output by df. Note that this works for Use% values from 50% to 99%, but not 100% and above (which can happen due to the reserved space for the superuser). This is left as an exercise :-)

Now as for running this regularly, you can set VISUAL=vi and/or EDITOR=vi and then crontab -e will pop you into the editor you specified. Insert a line like

* * * * * /full/path/to/above/script

and save & exit. This will run the script once a minute. Once every ten minutes would be

0,10,20,30,40,50 * * * * /full/path/to/above/script

Make sure the script is executable with chmod +x /full/path/to/above/script.


I'll try to help without giving you the answer. That way you get to learn some other stuff!

First you'll probably want to save a bunch of commands as a script and run it with something like cron. You can do like while true ; do rm a.* ; sleep 60 ; done but it won't restart after a reboot. Cron jobs will. That line of code runs every minute and deletes a.*. Change 60 down to 1 if you want it every second. To make it more useful, look at your favourite command for disk space, use things like cut and grep to take certain fields and remove header lines (if there is no option to the basic space command just to output %space) e.g.

[max@fedora ~]$ df /tmp
Filesystem     1K-blocks   Used Available Use% Mounted on
/dev/sda6        4866748 202812   4419868   5% /tmp
[max@fedora ~]$ df /tmp | grep tmp| cut -c43-45
  5

Then you just need to look at some if statement syntax and you're away. Save the commands that you write into a file called "space_clear.sh" and then add space_clear.sh to cron to run every minute and you're away. TOP TIP : While you're testing things out, do ls a.* instead of rm a.* and then if you get it horribly wrong, it's just showing you the files it would have deleted instead of deleting them! Remember, getting things wrong is learning. (If you're really still stuck, read about if, then and fi for your preferred shell (bash/ksh csh/bourne). Stick in the df command with extras in back quotes with a comparison operator and 50 as part of the "if".)


Need Your Help

NSPredicate checking certain String

ios iphone nsstring nspredicate predicatewithformat

I m working on a code which was implemented by someone else which is as follows

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.