[Ma-linux] Re: [Novalug] weekly cron job
Geoff McNamara
geoffm at companionway.net
Fri Jan 4 11:22:34 EST 2008
find ./* -prune -mtime +12 # will list files older than 12 days in the
current directory only (this is a safer method than find ./ -mtime +12
which finds all files recursively through all subdirectories) (you could
also use the gnu find argument -maxdepth=0 to prevent recursive finds)
By the way find has an file long listing option (-ls) so your command
find -type f -atime +12 | xargs ls -l
can be done with
find -type f -atime +12 -ls
There are several ways to do the removes
find ./* -prune -mtime +12 -exec rm {} \;
find ./* -prune -mtime +12 | xargs rm
rm `find ./* -prune -mtime +12`
Personally I alsway run a remove command with an echo infront of it before
as a test hence
find ./* -prune -mtime +12 -exec echo rm {} \;
find ./* -prune -mtime +12 | xargs echo rm
echo rm `find ./* -prune -mtime +12`
Then remove the echo once you are sure it is what you want and only what
you want!
-g-
> With Karri's help, I think I have this problem mostly solved. I am
> concerned
> about the use of the find command line :
>
> find -type f -atime +12 | xargs ls -l
>
> I know that I have 10 spam emails for the month of August in my spam
> directory. If I use the above command, I either don't get any of the
> files
> returned, or I get some subset of the 10, like 7.
>
> If I change the command to:
>
> find -type f -mtime +12 | xargs ls -l
>
> I was able to pickup all 10 files.
>
> What I am trying to accomplish with this command is to remove/delete from
> the
> spam directory all spam emails (files) older then 12 days.
>
> I believe I should run the mtime option as opposed to the atime option,
> but
> I'm wondering if this will do for me what I want.
>
> Any comments of suggestions?
>
> Thanks again Karri, for helping me out last night.
>
> Jay
>
>> John,
>>
>> Your info below was great, doing an 'ls /var/spool/cron/lastrun showed
>> me:
>>
>> email:/var/spool/cron/lastrun # ls -l
>> total 0
>> -rw-r--r-- 1 root root 0 Jan 3 01:00 cron.daily
>> -rw-r--r-- 1 root root 0 Dec 30 16:30 cron.weekly
>>
>> Ok, so cron.weekly ran on Dec 30. I have a script below, which Don Jr.
>> has
>> helped me with.
>>
>> The script should write out a message to a file called spam-learn
>> located at
>> /home/user-name which should state what date the script ran.
>>
>> Only problem is that the spam-learn file doesn't have any information
>> since
>> Dec 9th, which is when I changed from an old script, to the script
>> below.
>>
>> Also, the script should delete any spam emails in a particular folder
>> when
>> they are older then 12 days. But, when I look in the designated spame
>> folder,
>> I still have (all the) spam emails I placed there since Aug 24, 2007.
>>
>> I need some help figuring out why this is not working. Also, if I run
>>
>> find -type f -atime +12 | xargs ls -l
>>
>> I am not getting all files that meet this criteria. For example, in Aug
>> 2007
>> I stored 10 spam emails, running this command only shows I received 7
>> spam
>> emails in Aug.
>>
>> Jay
>>
>> ----------begin script (this line not included in file)
>> #!/bin/sh
>> PATH=/usr/local/bin:/bin:/usr/bin
>> set -e
>>
>> function dothejob() {
>> WHO="$1"
>> HOME=/home/${WHO}
>> SPAMDIR="${HOME}/MailDir/$2"
>> CD $HOME
>>
>> find ${SPAMDIR} -type f -print > /tmp/spamfiles
>> chown ${WHO} /tmp/spamfiles
>>
>> # --pidfile /dev/null so it always starts;
>> # sa-learn isn't really a daemon,
>> # but we want to start it like one so that
>> # it runs with the correct userid.
>> /sbin/start-stop-daemon -start --pidfile /dev/null \
>> --startas /usr/bin/sa-learn --oknodo --chuid $WHO \
>> -- --spam -f /tmp/spamfiles
>>
>> # log that we did it.
>> echo -e "sa-learn ran on \n$(date)" >> ${HOME}/spam-learn
>>
>> # Give owner to user to handle the case where 'spam-learn' was
>> deleted
>> chown $WHO ${HOME}/spam-learn || /bin/true
>> chmod rw,r,r ${HOME}/spam-learn || /bin/true
>>
>> # same usage as above of start-stop-daemon is going on here
>> /sbin/start-stop-daemon --start --pidfile /dev/null \
>> --startas /bin/sh \
>> --oknodo --chuid $WHO \
>> -- -c "find ${SPAMDIR} -type f -atime +12 | xargs rm -f"
>>
>> echo -e "deleted files older than 12 days" >> ${HOME}/spam-learn
>> date >> ${HOME}/spam-learn
>>
>> rm /tmp/spamfiles
>>
>> # find ${SPAMDIR} -type f -atime +12 | xargs rm -f
>> # echo -e "deleted files older than 12 days" >> ${HOME}/spam-learn
>>
>> }
>>
>> dothejob "jhart" ".knownspam/cur"
>> dothejob "llumbert" ".spam/cur"
>>
>> exit 0 # tell cron that everything went OK.
>>
>> #-----------------------end script
>>
>>
>>> "Jay Hart" <jhart at kevla.org> writes:
>>>
>>>> I want this to run as root, so I did in fact drop it in
>>>> /etc/cron.weekly.
>>>>
>>>> Since I rebooted the box, its been five days, so I will wait the two
>>>> more
>>>> days
>>>> I think need to pass prior to this script running.
>>>>
>>>> File is located in /etc/cron.weekly, has executable permissions, and
>>>> is
>>>> owned
>>>> by root.
>>>
>>> The /etc/cron.$TIMEPERIOD scripts are run by a script called
>>> 'run-crons' which is called periodically (every 10 or 15 minutes,
>>> usually) out of /etc/crontab.
>>>
>>> On the SLES systems at $WORK, run-crons lives in /usr/lib/cron; on
>>> the
>>> Gentoo boxes at home, it's in /usr/sbin -- locate is probably your
>>> friend.
>>>
>>> I suspect if you do a 'rm /var/spool/cron/lastrun/*' you'll see those
>>> longer period cronjobs execute in short order.
>>>
>>> (Now, if you're running anacron, all bets are off, but start by
>>> looking in /etc/crontab and you'll probably be able to puzzle it
>>> out.)
>>>
>>> john.
>>> --
>>> [The Clipper Chip scheme] "is a focal point for the distrust of
>>> goverment."
>>> - Clinton Brooks, NSA scientist who led the Clipper Chip project,
>>> _Wall_Street_Journal_ interview, 02/22/94. [No kidding, Clint.]
>>>
>>
>>
>>
>
>
> _______________________________________________
> Ma-linux mailing list
> Ma-linux at calypso.tux.org
> http://calypso.tux.org/cgi-bin/mailman/listinfo/ma-linux
>
More information about the Novalug
mailing list