[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [bash] script (redirect output) to file and email



* Jochen Schulz <ml@well-adjusted.de> schrieb:
> Pol Hallen:
> > 
> > I try to redirect output of this script to file (using tee) and also send
> > it to mail
> > 
> > the script deletes files older than 150days:
> > 
> > find /share/.trash/ -type f -atime +150 -exec rm -fr {}  \;
> 
> - atime finds files *last accessed* 150 days ago. You probably need
>   mtime instead.

Depends on the access patterns of these files. Maybe they're created
somewhen, but might be read later. In this case, -atime is correct.

> I am not even sure whether running the script will change the atime
> of all files in the directory and thus prevent anything in it to
> be deleted.

IMHO should not to. find does only directory scan and stat()'s, but
doesnt open() the files, so atime shouldn't be touched. (actually,
haven't checked yet).

> You should make sure that the filesystem is not mounted with
> "relatime" or "noatime" as well when using atime.

ACK. Note that this is done quite rarely, since it puts more load
onto the medium (metadata's touched each time a file's open()ed).
Maybe inotify is a better solution.

> - You are starting an rm process for every file to delete. You can end
>   the command with "+" instead of "\;" to make find pass as many files
>   to rm as possible. If you delete many files that way, that may make a
>   big difference spped-wise.

But you have to be careful that the command line doesnt get too long.
If you're really concerned about perfomance, you could use busybox
(with all commands compiled-in) as shell do write your script this way:


    do_rm() {
	while read FILE ; do rm -fr $FILE ; done
    }

    find /share/.trash/ -type f -atime +150 | do_rm


That will only require one or two fork()'s.

> - Do you want to have the list mailed as an attachment? -Then I would
>   just redirect the output to a file and use mutt's batch mode to send
>   the file (mutt -x -a $file).

If you have a local sendmail command, you could simply pipe it there.

    (
	echo "From: root@my.box.org"
	echo "To: myself@foo.org"
	echo "Subject: garbage collection output"
	echo
	
	find...
    ) | /usr/sbin/sendmail -t


cu
-- 
----------------------------------------------------------------------
 Enrico Weigelt, metux IT service -- http://www.metux.de/

 phone:  +49 36207 519931  email: weigelt@metux.de
 mobile: +49 151 27565287  icq:   210169427         skype: nekrad666
----------------------------------------------------------------------
 Embedded-Linux / Portierung / Opensource-QM / Verteilte Systeme
----------------------------------------------------------------------


Reply to: