Monitor directory and email


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Monitor directory and email
# 1  
Old 06-26-2009
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  
Old 06-30-2009
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  
Old 07-06-2009
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  
Old 07-06-2009
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  
Old 07-06-2009
and what if i would like to run it every 5 minutes?
# 6  
Old 07-06-2009
read your crontab's man page for that.
# 7  
Old 07-06-2009
Hey Thanks.....got it done as
Code:
*/5 * * * * /home/xyz/script.sh

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to monitor size in directory

i'm trying to find the most efficient way to monitor specific files in different directories in one go. /var/log/ /var/app/ /var/db/ each one of these directories can have subdirectories which need to be looked into as well. I want to find any file in this directory that has the name... (7 Replies)
Discussion started by: SkySmart
7 Replies

2. Shell Programming and Scripting

Script to monitor directory size of specific users

Hi, i am new to shell scripts, i need to write a script that can monitor size of directory of specific users. Please help. Thanks, Nitin (2 Replies)
Discussion started by: nicksrulz
2 Replies

3. Shell Programming and Scripting

monitor(audit) and log changes inside directory

Hi, everyone I would like to write ksh script in ksh (HP-UX), to audit any changes inside target directories. My enviroment has many constrains, so I can only use ksh and cannot install any 3rd party soft or command (include perl or other languages) The script functions like below 1) take a... (1 Reply)
Discussion started by: stev.h
1 Replies

4. Shell Programming and Scripting

howto monitor a directory for files then sftp them

Morning all I hope I have put this in the correct forum. I have a requirement to monitor a directory on a server for files being sftp'ed in and then to sftp them of to another server. The issues I have though of are making sure the files have completely transferred onto the server before they... (6 Replies)
Discussion started by: ltodd2
6 Replies

5. Shell Programming and Scripting

Directory Monitor in KSh

Hi, I need to write a directory monitor, i.e. a korn shell script which would Report changes to the directory contents, like: added file1, deleted file2, updated file3 , created subdir (optional)... There is no specific file pattern. So far I have written a little script... (1 Reply)
Discussion started by: olegkon
1 Replies

6. AIX

monitor directory events

I'm am looking for a cheap way to trigger a script when a new file is written in a specific directory. AIX 5.3. It is a production system, so no kernel patching (i.e. inotify). Filemon and audtiing are too expensive. Thanks in advance. (2 Replies)
Discussion started by: pbillast
2 Replies

7. Shell Programming and Scripting

Monitor capacity of directory

Good morning. I have been attempting to find a way to monitor the capacity of a directory so that when it reaches 80% or higher I can send an event. I was able to find a script that does this for the whole drive by I can not seem to figure out how to do this for just a single directory. ... (1 Reply)
Discussion started by: LRoberts
1 Replies

8. Shell Programming and Scripting

script to monitor directory

What is the best way for a script to run to monitor a directory for the presence of files and then perform a function afterwords? I was hoping to have it continually run and sleep until it detects that files are present in the directory, then break out of the loop and go on to the next step. ... (17 Replies)
Discussion started by: nulinux
17 Replies

9. UNIX for Dummies Questions & Answers

Hep with script to monitor directory

Hello, I am a newbie who is attempting to write a script to monitor a directory for a set of 3 files that I am expecting to get ftp'd. Occasionally, we suspend operations for maintenance etc. but we still get the files so there can be more than 1 set. If there is more than 1 set, I would like... (2 Replies)
Discussion started by: cmf00186
2 Replies

10. Programming

Monitor which users enter my home directory

Hi, I would like to monitor which users enter my home directory. Is it possible to write a script or code to do this. I donot have admin privileges. I have given read permissions to access my home directory. Any pointers in this direction is helpful! Thanks, Pradeep Ps: I use the... (1 Reply)
Discussion started by: mnpradeep
1 Replies
Login or Register to Ask a Question