EHLO!
This started like a "hey, give me 30 minutes" but here I am a day after. Basically what I want to do is to check "/var/log/messages" for 30 seconds and when someone plug-in a USB hard disk during that time, execute some commands.
So I am using Per function "File::Tail", and kinda works but I don't know how to run it for just 30 seconds, tail stays there until I manually exit( It works just as tail command). I been looking for solutions using fork but still no luck or experience on it. Part of the "tail" code is like this:
while ((defined($line=$file->read)) ) {
I appreciate any help or tip. If anyone has another solution using another, function or what ever, it's fine, I need the code for a "CGI".
You can achieve this using File::Tail::select() .
The example below puts one seconds worth of the tailed file into the @pending
array which is then processed. Looping 30 times will give you very close to 30 seconds (dependend on how active your /var/log/messages is). If you require more precise timing you can store the time before the loop and compare how long has passed on each iteration.
use File::Tail;
$file = File::Tail->new(name=>"/var/log/messages", interval=>1);
TIMER_LOOP:
while($t++ < 30){
(undef,undef,@pending)=File::Tail::select(undef,undef,undef, 1, $file);
for(@pending){
if($_->read =~ /New USB device found/){
print "Hello USB!\n";
last TIMER_LOOP;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.