1

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.

2
  • Do you mean different servers, or do you mean different locations (directories) on the same server? Apr 25, 2012 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, 2012 at 12:09

2 Answers 2

0

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.

0

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

ssh [email protected] rm -rf /path/to/cache
ssh [email protected] rm -rf /path/to/cache2
etc.

Check for typos! :-)

Or safer:

ssh [email protected] '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:

ssh-copy-id [email protected]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .