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?
TbalzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
The simple way is to create a mail subroutine

To call the subroutine will pass the references to the recipient, subject, and message


sub mail_notification {
          my ($recipient,$subject,$message)=shift(@_);
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.
0
TbalzAuthor Commented:
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.
0
arnoldCommented:
When the script is triggered
Open DIR, "ls /path/to |" || die "unable to get listing\n";
While <DIR> {

Print "$_\n";
}
Close(DIR)
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

TbalzAuthor Commented:
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.

Open in new window

0
arnoldCommented:
the missing semicolon is on the close line.  You need to make every command lower case
open, while, print, close
0
TbalzAuthor Commented:
Oh ok thanks Now I have:

#!/usr/bin/perl
#
use warnings;
use strict;
#


open DIR, "ls /test/files |" || die "unable to get listing\n";
while <DIR> {

print "$_\n";
}
close(DIR);

Open in new window


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.

Open in new window

0
TbalzAuthor Commented:
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?

#!/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;
~         

Open in new window

0
arnoldCommented:
You can use rename file /path/to/newlocation/file.
You can use system ("mv file ./directory/file");
If you are only scanning and moving, using a bash script might be simpler

ls |while read filename; do
echo "$filename"
mv "$filename" "new/$filename"
done

A similar option to mail out exists in bash/ksh/sh
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.