7 Einträge, 1 Seite |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#!/usr/bin/perl -w use constant FIFOPATH => "/tmp/fifo-demo"; use POSIX qw(mkfifo); use Fcntl qw(O_RDONLY); use Sys::Syslog qw( :DEFAULT setlogsock); # catch signals to die gracefully $SIG{'TERM'} = 'handler'; $SIG{'INT'} = 'handler'; # syslog sub do_syslog { my $msg = shift; setlogsock('unix'); openlog($0,'','user'); syslog('info',"$msg"); closelog; } sub handler { my $signal = shift; # signal-nummer besorgen $SIG{'TERM'} = 'handler'; # reinstall sig-handler $SIG{'INT'} = 'handler'; # reinstall sig-handler &do_syslog ("Signal: SIG$signal caught!"); unlink(FIFOPATH); exit 0; } sub do_notification { my $string = shift; &do_syslog($string); # instead of this 'sleep' here should be some intelligent code to # do magic stuff with the data we got from uncle fifo. sleep 5; } ########################### MAIN ################################ unlink(FIFOPATH); mkfifo(FIFOPATH, 0666) or die "can't create FIFO: $!\n"; sysopen(FIFOFD, FIFOPATH, O_RDONLY) or die "can't read: $!\n"; #my $fifostring = ""; #while (1) { # if (sysread(FIFOFD, $fromfifo, 1) == 1) { # if ($fromfifo eq "\n") { # &do_notification($fifostring); # $fifostring = ""; # } else { # $fifostring .= $fromfifo; # } # } #} # easyer reading line-by-line while (1) { while (<FIFOFD>) { &do_notification($_); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/usr/bin/perl -w # fifo-client.pl -- Sendet Auftraege in eine FIFO hinein. use strict; use Data::Dumper; my $fifo = "/tmp/fifo-demo"; my $string = shift; #sysopen(FIFOFD, FIFOPATH, O_WRONLY) or die "can't write: $!\n"; open (FIFOFD, ">$fifo") or die "could not open fifo: $!\n"; print FIFOFD "$string\n"; close (FIFOFD);
while(1) { ... }
close (FIFOFD);
7 Einträge, 1 Seite |