Tbalz
asked on
Perl Script to send notification email once file is copied to linux directory
Hi Guys,
I'm looking to write a Perl script which i will setup as a cronjob in Linux. I wan the script to monitor a directory and to send a notification email via sendmail once a new file is copied/created into the directory. Can someone give me an example of how this can be done in Perl?
I'm looking to write a Perl script which i will setup as a cronjob in Linux. I wan the script to monitor a directory and to send a notification email via sendmail once a new file is copied/created into the directory. Can someone give me an example of how this can be done in Perl?
ASKER
I would like to use a hash if possible. But after a file is copied into the directory , I would like it to be moved somewhere else. So basically this directory will always be empty. Would it be easier to monitor this directory that way ? I would like to see an example how a directory could be monitored for an coming file before the email subroutine can be triggered.
When the script is triggered
Open DIR, "ls /path/to |" || die "unable to get listing\n";
While <DIR> {
Print "$_\n";
}
Close(DIR)
Open DIR, "ls /path/to |" || die "unable to get listing\n";
While <DIR> {
Print "$_\n";
}
Close(DIR)
ASKER
I get the following error when i try to run the above code in a script.
String found where operator expected at ./listFiles.pl line 11, near "Print "$_\n""
(Do you need to predeclare Print?)
Semicolon seems to be missing at ./listFiles.pl line 12.
syntax error at ./listFiles.pl line 9, near "DIR>"
Execution of ./listFiles.pl aborted due to compilation errors.
the missing semicolon is on the close line. You need to make every command lower case
open, while, print, close
open, while, print, close
ASKER
Oh ok thanks Now I have:
but i still get the error:
#!/usr/bin/perl
#
use warnings;
use strict;
#
open DIR, "ls /test/files |" || die "unable to get listing\n";
while <DIR> {
print "$_\n";
}
close(DIR);
but i still get the error:
syntax error at ./listFiles.pl line 9, near "while <DIR>"
Execution of ./listFiles.pl aborted due to compilation errors.
ASKER
I think i got it the way i want it. However, i'm not sure how to move the files i have in the $files variable?
Also, if there are no files in this directory. Will the script just stop or do i need to put something in?
Also, if there are no files in this directory. Will the script just stop or do i need to put something in?
#!/usr/bin/perl
use strict;
use warnings;
my $dir = '/test/files';
opendir(DIR, $dir) or die $!;
while (my $file = readdir(DIR)) {
# Use a regular expression to ignore files beginning with a period
next if ($file =~ m/INCOMING/);
next if ($file =~ m/OUTGOING/);
next if ($file =~ m/^\./);
print "$file\n";
}
closedir(DIR);
exit 0;
~
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To call the subroutine will pass the references to the recipient, subject, and message
sub mail_notification {
my ($recipient,$subject,$mess
open MAIL "|/usr/sbin/sendmail -oi -t" || die "unable to open sendmail:$!\n";
print MAIL <<EOF
To: $$recipient
From: <your emailaddress>
Subject: $$subject
@$message
EOF
;
close(MAIL);
} #end mail_notification sub
Presumably to maintain the state of the directory I.e. The script will maintain which files were present to detect new files, you should consider using a Flat file DB, dbm, or a hash.