Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What would be the best way to manage a cron job which deletes files from multiple locations every week?

When I say manage, I mean the different locations ideally this would work best in a MySQL database. Without creating a cron job for each location.

The reason for this is that we have a number of different website projects which generate cached files and over the past few weeks have started to grow in size.

share|improve this question
Do you mean different servers, or do you mean different locations (directories) on the same server? – EightBitTony Apr 25 '12 at 11:09
Just create a single shell script that does the job and place that in cron. The different locations you want to clear could either be included in the script or be read from an external file. – m000 Apr 25 '12 at 12:09
up vote 0 down vote accepted

If you mean different servers, then creating a cronjob on each is the best thing to write. Write a common script which does useful housekeeping, and schedule it on all the servers.

If you mean different locations on the same server, then one cronjob that reads directories from a text file and cleans them is the simplest option, mysql seems overkill for a list of target directories. If there is a good reason for having the locations in a mysql database, then you could write a single perl script to read the mysql data and tidy up the directories listed.

Lastly, depending on how your web applications create the files, they should be written to housekeep them themselves anyway. When the application creates a new cached file, it could check to see if it needs to remove any. Self managing applications are better than relying on external scripts which can fail for unrelated reasons.

share|improve this answer

You could create a script which runs on one server through cron and will use ssh to delete files at various locations:

ssh rm -rf /path/to/cache
ssh rm -rf /path/to/cache2

Check for typos! :-)

Or safer:

ssh 'cd /path/to && rm -rf cache'

Make sure to use ssh-copy-id to copy your key to the remote servers so you can use password-less log in:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.