The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   UNIX - General (http://hintsforums.macworld.com/forumdisplay.php?f=16)
-   -   find and email script (http://hintsforums.macworld.com/showthread.php?t=35547)

gowrann 02-23-2005 07:15 AM

find and email script
 
Hi there, can someone help me with a script - I want to set up a cron job that searches every 5 mins for files starting QXP- * on a large server.

I need the search to traverse the directory structure and return the path and perms of any files starting with this name via email to particular users.

weltonch777 02-23-2005 11:45 AM

It would be something along the lines of:

Code:


rm -f /tmp/mailqbx.tmp

for filename in $(find / -type f -name QXP-*)
do
echo $filename >> /tmp/mailqbx.tmp
ls -al $filename 2>/dev/null >> /tmp/mailqbx.tmp
echo >> /tmp/mailqbx.tmp
done

cat /tmp/mailqbx.tmp | mail -s "QBX Report for $(date)" someone@somewhere

rm -f /tmp/mailqbx.tmp

ls -al will give you the name, perms, owner, ect. Find will give you the complete path. Please note however, that if you do this over the entire file tree every five minutes, that's about all this computer will be capable of doing. (Massive system slowdown :( )

gowrann 02-23-2005 09:54 PM

Hi Weltonch - almost there just need to fix message body - any ideas

localhost:~/Desktop traffic$ /usr/local/bin/qxp.pl
find: /Volumes/Server 2//Network Trash Folder/Trash Can #2: Permission denied
find: /Volumes/Server 2//Network Trash Folder/Trash Can #5: Permission denied
find: /Volumes/Server 2//Network Trash Folder/Trash Can #6: Permission denied
find: /Volumes/Server 2//Network Trash Folder/Trash Can #7: Permission denied
cat: /tmp/mailqbx.tmp: No such file or directory
Null message body; hope that's ok

weltonch777 02-23-2005 10:45 PM

It means your temp file wasn't created. Nothing to report? Try running a find without piping it to anything, and see if there's anything to send.

forbin 02-23-2005 10:48 PM

Use sudo .. You should be admin when you run this ..

You can also do this as a one liner (untested .. but it should work) ..

sudo find / -type f -name 'QXP-*' | xargs ls -al | mail -s "QBX Report for $(date)" someone@somewhere

weltonch777 02-23-2005 10:52 PM

Quote:

Originally Posted by forbin
Use sudo .. You should be admin when you run this ..

You can also do this as a one liner (untested .. but it should work) ..

sudo find / -type f -name 'QXP-*' | xargs ls -al | mail -s "QBX Report for $(date)" someone@somewhere

He'll lose the full path if he does it like that. And also, I'm not positive, but I think he'd end up sending a seperate email for each file found. That could be annoying. Your right about the sudo part, though.

hoops 02-23-2005 10:59 PM

OK after trying the third time - its starting to work and output via email as:

/Volumes/Server

2//testing/95000

Work/QXP-Coke

/Volumes/Server

2//testing/QXP-neiltest.txt

however no user and perm listing - is this possible?

weltonch777 02-23-2005 11:04 PM

Did your name change? And yes, it is possible. Gonna hafta figure out a workaround quick. Bash sees white space as a delimiter. Your running ls -al on broken up file names.

weltonch777 02-23-2005 11:32 PM

This one will take full lines.

Code:

rm -f /tmp/mailqbx.tmp 2>/dev/null
rm -f /tmp/qbxtemp.tmp 2>/dev/null

echo QBX Report for $(date) >> /tmp/mailqbx.tmp
echo >> /tmp/mailqbx.tmp

echo "$(find / -type f -name QXP-*)" > /tmp/qbxtemp.tmp

filenum=$(grep -c . /tmp/qbxtemp.tmp)
count=1

while [ $count -le $filenum ]
do
echo "$(head -${count} qbxtemp.tmp | tail -1)"  >> /tmp/mailqbx.tmp
ls -al "$(head -${count} qbxtemp.tmp | tail -1)" >> /tmp/mailqbx.tmp
let count=$count+1
done

cat /tmp/mailqbx.tmp | mail -s "QBX Report for $(date)" someone@somewhere

rm -f /tmp/mailqbx.tmp 2>/dev/null
rm -f /tmp/qbxtemp.tmp 2>/dev/null


gowrann 02-23-2005 11:33 PM

Yes my name did change - first tried to run as root got the email but no body, then su to an admin account and the body text is displayed

weltonch777 02-23-2005 11:37 PM

That was because of the permissions on the trash cans....

ind: /Volumes/Server 2//Network Trash Folder/Trash Can #2: Permission denied
find: /Volumes/Server 2//Network Trash Folder/Trash Can #5: Permission denied
find: /Volumes/Server 2//Network Trash Folder/Trash Can #6: Permission denied
find: /Volumes/Server 2//Network Trash Folder/Trash Can #7: Permission denied


Here, let me edit my last one quick so that doesn't happen, even if there is nothing to find.

Done! Edited my last one so that even if you don't find anything, the message won't be completely empty. :D

gowrann 02-24-2005 12:12 AM

ok command line output:

localhost:~ traffic$ /usr/local/bin/qxp2.pl
find: /Volumes/Server 2//Network Trash Folder/Trash Can #2: Permission denied
find: /Volumes/Server 2//Network Trash Folder/Trash Can #5: Permission denied
find: /Volumes/Server 2//Network Trash Folder/Trash Can #6: Permission denied
find: /Volumes/Server 2//Network Trash Folder/Trash Can #7: Permission denied
head: qbxtemp.tmp: No such file or directory
head: qbxtemp.tmp: No such file or directory
ls: fts_open: No such file or directory
head: qbxtemp.tmp: No such file or directory
head: qbxtemp.tmp: No such file or directory
ls: fts_open: No such file or directory

mail output (no listings):
QBX Report for Thu Feb 24 16:07:32 EST 2005

weltonch777 02-24-2005 12:18 AM

Did you not run as sudo, or as the admin you were last time you got text?
...EEK ! My Bad!!

forgot /tmp/ :o

weltonch777 02-24-2005 12:27 AM

Code:

rm -f /tmp/mailqbx.tmp 2>/dev/null
rm -f /tmp/qbxtemp.tmp 2>/dev/null

echo QBX Report for $(date) >> /tmp/mailqbx.tmp
echo >> /tmp/mailqbx.tmp

echo "$(find / -type f -name QXP-*)" > /tmp/qbxtemp.tmp

if [ -f /tmp/qbxtemp.tmp ]
then
filenum=$(grep -c . /tmp/qbxtemp.tmp)
else
filenum=0
fi

count=1

while [ $count -le $filenum ]
do
echo "$(head -${count} /tmp/qbxtemp.tmp | tail -1)"  >> /tmp/mailqbx.tmp
ls -al "$(head -${count} /tmp/qbxtemp.tmp | tail -1)" >> /tmp/mailqbx.tmp
let count=$count+1
done

cat /tmp/mailqbx.tmp | mail -s "QBX Report for $(date)" someone@somewhere

rm -f /tmp/mailqbx.tmp 2>/dev/null
rm -f /tmp/qbxtemp.tmp 2>/dev/null

Sorry bout that! ;)

gowrann 02-24-2005 05:18 AM

thousand thanks weltonch

'......just like a bought one!".........

acme.mail.order 02-25-2005 04:40 AM

Holy Complexity Theory Batman !! :eek:

try this one-liner:

Code:

sudo find / -type f -name "QXP-*" -exec ls -l {} \; | mail  -s "QBX report for `date`" my.address@server.com
returns the full path, owner, permissions and puts it in a single mail message. You would only get separate messages if you had a loop that invoked mail per iteration. find runs `ls` in a loop, but it's piped to mail all in one go.

I don't exclude zero results, but if he's doing regular searches like this then absences would seem significant, no?

And what's with the `head -n | tail -1` business? If you want line 50 of a file, use `sed -n '50p'` or similar in the pipeline. For ranges, `sed -n '50,60p'`


gowrann: as mentioned in post #2 doing this every 5 minutes will bring your machine to it's knees. There are far, far more efficient methods available!

Thoughts:

Files should not appear at random throughout the filesystem. If some process is doing this, then it should be shot at the earliest opportunity. Limit the find to the appropriate directories.

Have the originating program do notifications by itself rather than cleaning up after it.

If users are doing this, they need a good, hard smack upside the head.

weltonch777 02-25-2005 07:34 AM

Yeah, I just figured that out for sed today. I was trying something similar to yours, but could not get it to work (hence the tmp files), and once I got tmp files, I was pretty much screwed.

..Ok, now I feel like a real moron. Just got it to work correctly with xargs too...

gowrann 02-25-2005 08:59 AM

Thanks acme I have actually narrowed down the search to a specific location and cron'ed it to 15 mins - processor time hits 50% for 2.5 secs which I can live with.

The issue is errant temp files left by Quark 6.5, named QXP-12212154.qxp which actually supercede the orginal file. The errant files are produced by some reproducible steps but some I am still working out why - when I have 35 operators working on the server then I need to know when the temp files are happeing and by who.

gowrann 02-27-2005 10:43 PM

ok last thing I need to sort is the cron schedule.

Currently I have Cronnix installed and have added the qxp.sh (the above script) as a new task set to go every 15 mins.

I have made the file executable and have amended my path so the command can just be typed - however it is not executing in cron but will if I do a once off command from the terminal.

/var/cron/tabs/user (content is:)

10 * * * * qxp3.sh
what is wrong here?

weltonch777 02-27-2005 11:42 PM

Try putting the full path in the cron tab. Just because you added it to your path doesn't mean it was added to the root logon shell's path (or whatever user Cronnix employs)

To get info on where Cronnix is comming from, make a sh file called Cronnix.info.sh and put it in your / (root) path.

Have it contain..

Code:

$echo "$USER
$PATH
$SHELL" > /MyCronnix.log

Run it from a cron tab and it will contain all the info on what is running the tasks.

logfile should look something like this..
Code:

username
/sw/bin:/sw/sbin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:./
/bin/sh

Chris

gowrann 02-28-2005 08:13 AM

Output:
Code:

macserver:/ root# /Cronnix.info.sh
/Cronnix.info.sh: line 3: root
/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin
/bin/sh: No such file or directory


weltonch777 02-28-2005 08:30 AM

So, it's running as root, using sh, and these are the path's it is checking:

/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin

If your file is not in one of those paths, it will not find it. I would suggest putting it in /usr/local/bin , or editing the PATH declaration in /etc/profile to include your script directory. Make sure to type . profile
after you change it, or nothing will happen till you reboot ( that's DOT SPACE profile , while inside the etc directory)

gowrann 02-28-2005 10:55 PM

use of:
Code:

. /
fixed it

gowrann 04-19-2005 10:31 PM

One more addition
 
Hi this works for me just great - however what code do I need if I only want the script to email me if the search finds something.

If the search is blank then I do not want an email.


All times are GMT -5. The time now is 06:21 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.