• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 419
  • Last Modified:

Need cron job to delete files every month first week Saturday.

Hi experts,

Below is the my corn job Not working :-(. Its deleting file ever Saturday.

0 1 1-7 1-12 6 rm /srv/www/upload_doc/*

My requirement is Every moth first week of Saturday only the files need to be deleted. In above cron stanza what i m missing. pleas help.


2 Solutions
This usual solution is something like, first write a wrapper script that will run somecommand only if the day of the month is 7 or less:

#! /usr/bin/ksh
day=$(date +%d)
if ((day <= 7)) ; then
   exec somecommand
exit 1

Open in new window

Then run the wrapper every Saturday: "0 0 * * 1 wrapper"
Or all in one:

0 1 * * 6 [ $(date "+\%d") -le 7 ] &&  rm /srv/www/upload_doc/*

Note the backslash ("\") in front of the percent ("%") sign! It's very important under cron.

If you'd like to test on the command line however, you must remove it, since outside cron it will be considered an error.

MaddyUNIXAuthor Commented:
As always wmp rocksssssssss
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now