set up a small maintenance program on a server

I have a small webapplication (PHP and XSLT based) which uses an intern file cache and saves the user’s search data.

Now, I don’t want to run out of disk space, so I need a program for cleaning up and warning. It should do the following:

  • start once every 24 hours
  • check the files in the intern cache directory and delete all files which are older than 1 hour
  • sum up the whole disk space used and if it is greater than 1 gb, send an email to the admin.
  • stop

So, I think the programming is rather simply and I think I can cope with that (I know a little bit C++ and PHP). But I have no idea how to set up this program in an efficient way so that it only starts once every 24 hours and stops immediately after it has done its tasks: Which program language should I use? Is there a certain framework for programs like this?

My system is openSUSE 13.1 64 bit, the webserver is Apache 2.4.6.

Answer

Typically this is a job where you wouldn’t write a program at all, just a short shell script called once per day via a cron job that does it jobs with standard tools like find:

#!/bin/bash 
find /path/to/cache -type f -cmin +60 -delete 

called via a cron job will handle the deletion part. Make this executable and copy it over to /etc/cron.daily to have it executed once a day.

Check that this does the right thing beforehand by editing out the -delete part.

sum up the whole disk space used and if it is greater than 1 gb, send an email to the admin.

It’s not clear what you want to sum up, so I leave that out for now, but du or df show disk usage. du -s /path/to/cache would sum up the cache size

Additionally, this is not nearly all what you want to monitor on your system to keep it healthy. Checking (and rotating) log files, overall disk space etc.etc. are important just as much, let alone keeping the system up to date.

Attribution
Source : Link , Question Author : cis , Answer Author : Sven

Leave a Comment