News:

Go to HostNed.com Home
Welcome.  This is a place to get user-to-user support, learn more, and share ideas.  If you can't find your answers here, feel free to ask by creating a new topic or visit the support ticket system at https://my.hostned.com :)  Have fun here!

Main Menu
Welcome to HostNed Community. Please login or sign up.

Shoutbox


Cron Jobs

Started by avatarweb, May 02, 2007, 10:30:55 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

avatarweb

Hi Mr Dynaweb (as it looks like you who always replies to these things, but hello to anyone else who wants to help too:)

I want  to add a cron job to run every day, which deletes all files in a certain directory that are more than 3 days old.

I have gone to the Cpanel, and adding a job looks fairly straightforward, but before entering commands I want to make sure I know what I'm doing.

I have very little unix experience (only that which I have gained form running hosted websites for a few years), and want to make sure I know the commands that will filter the set of files being deleted to ONLY those older that 3 days.

As a side issue, these files are created by a php script running on the site, so I wonder about the permissions, and I wonder if there is a heirarchy to how permissions are structured. (for example is there a user 'apache, or php who may be the owner of the files?)

Dynaweb

#1
Hi,

This can be done through cron.

*** Updated 4/23/25 for new compatibility and details ***
Here's How to Add the Cron Job You Described


To delete files older than 3 days in a specific directory (e.g.
/home/username/tmpfiles), you can use the
find command in your cron job like this:

find /home/username/tmpfiles -type f -mtime +3 -delete
Explanation:
  • find: The command to search files.
  • /home/username/tmpfiles: Replace this with the actual directory path.
  • -type f: Only looks for files (not directories).
  • -mtime +3: Only selects files modified more than 3 days ago.
  • -delete: Deletes them.
IMPORTANT: Make absolutely sure you've tested this in a safe way (e.g. remove
-delete and add
-print to test which files would be deleted).

🕑 How to Set This to Run Daily
In the cPanel Cron Jobs section, under "Add New Cron Job", choose:
  • Common Settings:
    Once Per Day
  • Command:
    find /home/username/tmpfiles -type f -mtime +3 -delete
(Again, replace
/home/username/tmpfiles with the real path.)

Permissions

As for file ownership, our new cPanel servers should be configured to allow access without any problem. If you need help, feel free to open a support ticket.