The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Monitor capacity of directory LRoberts Shell Programming and Scripting 1 06-01-2009 11:58 AM
script to monitor files in a directory and sending the alert shellscripter Shell Programming and Scripting 0 11-26-2008 02:26 AM
script to monitor directory nulinux Shell Programming and Scripting 17 08-22-2008 10:28 AM
Hep with script to monitor directory cmf00186 UNIX for Dummies Questions & Answers 2 10-25-2006 02:42 PM
Monitor which users enter my home directory mnpradeep High Level Programming 1 03-21-2002 05:08 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-26-2009
dhawalkv dhawalkv is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Monitor directory and email

Hello all,

Can anyone please guide / help me in the following task....

I have a directory where some external users will upload pdf files. The filename of these pdf will be of a particular format (<id>-<first name>_<last name>_<some number>.pdf)
I want to make a script such that it takes (splits) the id from the filename and query the mysql database for the email address of that id (this can be done from a php script also) and then email on that particular id. Also this script has to run periodically (many times in a day...this can be done from cron).

I need some guidance on how to go about this.....whether this can be done from a shell script or php script and some help with the commands if shell script. My knowledge on PHP also is not very good, so if there are any inputs for using php then just guide me along.

Thanks....
  #2 (permalink)  
Old 06-30-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
Something like this?
Code:
cd $UPLOAD_DIRECTORY
find . -type f -newer timestamp -name "*.pdf" -print | 
   awk -F- { print $1 } | 
   while read id ; do 
        email_user_by_id $id
   done

touch timestamp
So the email_user_by_id does your SQL query and then sends email. (You can do it in a shell script, but I don't know what your db system is or your table layout, etc). The email part can be like this:
Code:
  /usr/sbin/sendmail -t  <<EOF
From: "The system" <otheus@xxxx.com>
To: $email
Subject: Your PDF file was received

blah blah blah
EOF
  #3 (permalink)  
Old 07-06-2009
dhawalkv dhawalkv is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Hey otheus thanks for the reply....
I took some points from your reply and from few searches in the forum got it working...
here is my script -

Code:
#!/bin/bash

UPLOAD_DIR="/home/xyz/test"
REPORT_DIR="/home/xyz"
UNAME="xyz"
PASS="xyz"
DB="xyz"
URL="http://www.xyz.com/pdf/"

cd $UPLOAD_DIR

for FILENAME in *.pdf
do
	USER_ID=`echo $FILENAME | cut -d "-" -f1`	
	EMAIL_TO=`echo $(mysql -u $UNAME -p$PASS $DB -e "select email from <tablename> where user_id=$USER_ID") | cut -d " " -f2`
	#echo "Send email to $EMAIL_TO"
	BODY="blah blah blah"
	#echo $BODY
	mv $FILENAME $REPORT_DIR
	/usr/sbin/sendmail -t  <<EOF
	From: a@b.com
	To: $EMAIL_TO
	Subject: <subject>
	Content-Type: text/html; charset=us-ascii
	$BODY
	EOF
done
and this is working. i manually run it and i get the email from this.
now when i schedule it in cron, it doesn't execute.....dont know why

here is the line i used in crontab
Code:
1 * * * * /home/xyz/script.sh
trying to run it every minute...but it doesnt get executed. and i'm not even using any environment variables....can you suggest whats wrong?

Thanks...
  #4 (permalink)  
Old 07-06-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
Your cron will run once an hour, at one minute past the hour. To run every minute you need:
Code:
* * * * * /home/xyz/script.sh
  #5 (permalink)  
Old 07-06-2009
dhawalkv dhawalkv is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
and what if i would like to run it every 5 minutes?
  #6 (permalink)  
Old 07-06-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,864
read your crontab's man page for that.
  #7 (permalink)  
Old 07-06-2009
dhawalkv dhawalkv is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 4
Hey Thanks.....got it done as
Code:
*/5 * * * * /home/xyz/script.sh
Sponsored Links
Bits Awarded / Charged to dhawalkv for this Post
Date User Comment Amount
07-06-2009 otheus for posting own solution 500
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0