![]() |
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. |
It would be something along the lines of:
Code:
|
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 |
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.
|
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 |
Quote:
|
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? |
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.
|
This one will take full lines.
Code:
rm -f /tmp/mailqbx.tmp 2>/dev/null |
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
|
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 |
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 |
Did you not run as sudo, or as the admin you were last time you got text?
...EEK ! My Bad!! forgot /tmp/ :o |
Code:
rm -f /tmp/mailqbx.tmp 2>/dev/null |
thousand thanks weltonch
'......just like a bought one!"......... |
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.comI 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. |
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... |
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. |
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? |
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 "$USERlogfile should look something like this.. Code:
username |
Output:
Code:
macserver:/ root# /Cronnix.info.sh |
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) |
use of:
Code:
. / |
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.