1

I have little experience with setting up cron jobs.

I need to configure a cron job to delete all files starting with the filename 'sess_' from the '/tmp' folder every hour.

If I fire up the crontab using crontab -e and write the following code:

0 */1 * * * find /tmp/ -name "sess_*" -delete

will that work?

Does the syntax look correct? Do I need to restart anything? (i.e - Apache)

Thanks in advance!

Bob Flemming
  • 1,175
  • 3
  • 13
  • 17

1 Answers1

2

The */1 is the same as * in the cron definition so

0 * * * *  ...

would work the same way.


In find the -delete will delete the files as you request but it may not be what you want. You can test your find command by running it from the command line without the -delete option

find /tmp/ -name "Sess_*"   

which will give you the list of files that would be acted upon.


Better still might be to look at the /etc/cron.d/php5 file which does the same thing for session files located in /var/lib/php5. Note that it only deletes files that are older than the system's maxlifetime by using the -cmin option ...

# /etc/cron.d/php5: crontab fragment for php5
#  This purges session files older than X, where X is defined in seconds
#  as the largest value of session.gc_maxlifetime from all your php.ini
#  files, or 24 minutes if not defined.  See /usr/lib/php5/maxlifetime

# Look for and purge old sessions every 30 minutes
09,39 *     * * *     root   [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete

By doing it this way you won't blindly delete session files that are in use.

user9517
  • 114,104
  • 20
  • 206
  • 289