Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use File::Monitor;
  6. use File::Basename;
  7. use Time::HiRes qw {usleep};
  8.  
  9. my $pid,@pids;
  10.  
  11.  
  12. sub textfile_notifier {
  13. my ($watch_name, $event, $change) = @_;
  14.  
  15. my @new_file_paths = $change->files_created; #The change object has a property called files_created,
  16. #which contains the names of any new files
  17. for my $path (@new_file_paths) {
  18. my ($base, $fname, $ext) = fileparse($path, '.log'); # $ext is "" if the '.txt' extension is
  19. # not found, otherwise it's '.txt'.
  20. if ($ext eq '.log') {
  21. print "$path was createdn";
  22. #-----------------------------------------Forking part #-----------------------------------------
  23. defined ($pid = fork()) or die "Couldn't fork: $!";
  24. if ($pid == 0) { #then in child process
  25. print "Loop got executedn";
  26. }
  27. else { #then in parent process, where $pid is the pid of the child
  28. push @pids, $pid;
  29. }
  30. #-----------------------------------------Forking part ends here #-----------------------------------------
  31. }
  32. }
  33. }
  34.  
  35. my $monitor = File::Monitor->new();
  36.  
  37. $monitor->watch( {
  38. name => '/home/goudarsh/Desktop/logs/',
  39. recurse => 1,
  40. callback => {files_created => &textfile_notifier}, #event => handler 1
  41. } );
  42.  
  43. $monitor->scan;
  44. while (1) {
  45. $monitor->scan; #Scanning the directory one
  46. #sleep(2);
  47. #usleep(10_000); #$microseconds = 750_000;
  48. for my $pid (@pids) {
  49. waitpid($pid, 0) #0 => block
  50. }
  51. }
  52.  
  53. touch 1.log
  54. touch 2.log
  55. touch 3.log
  56. touch 4.log
  57.  
  58. /home/goudarsh/Desktop/logs/1.log was created
  59. /home/goudarsh/Desktop/logs/2.log was created
  60. /home/goudarsh/Desktop/logs/2.log was created
  61. /home/goudarsh/Desktop/logs/3.log was created
  62. /home/goudarsh/Desktop/logs/3.log was created
  63. /home/goudarsh/Desktop/logs/3.log was created
  64. /home/goudarsh/Desktop/logs/4.log was created
  65. /home/goudarsh/Desktop/logs/3.log was created
  66. /home/goudarsh/Desktop/logs/4.log was created
  67. /home/goudarsh/Desktop/logs/4.log was created
  68. /home/goudarsh/Desktop/logs/4.log was created
  69. /home/goudarsh/Desktop/logs/4.log was created
  70. /home/goudarsh/Desktop/logs/4.log was created
  71. /home/goudarsh/Desktop/logs/4.log was created
  72. /home/goudarsh/Desktop/logs/4.log was created
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement