how to read tail -F command output in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to read tail -F command output in perl
# 1  
Old 05-13-2010
how to read tail -F command output in perl

Hi All,

How I will read the output of the tail -F command in perl.

I have text file with below contains

file1.txt
Code:
1
2
3
4

Code:
$running=1;
sub openLog($)
{
  (my $log) = @_;
  local *LOG;
  unless( open(LOG,$log." |") ) { print "unable to launch log: $!\n"; return 0; }
  print "logging started with ".$log."\n";
  return *LOG{IO};
}

while($running) {
my $pipe = openLog('tail -F file1.txt');
while (<$pipe>) { print $_ ;}
sleep 30;
}

After running the above script I am getting the below output and I am sending this output to the another server.
Code:
1
2
3
4

After that I have modified the file1.txt and added the below data.

file1.txt
Code:
1
2
3
4
5 #new data
6 #new data
7 #new data

I am getting the below output
Code:
1
2
3
4
5 #new data
6 #new data
7 #new data

How I will get below output after modifying the file1.txt , so I can send this output to the another server instead of sending again previous data
Code:
5 #new data
6 #new data
7 #new data

Please help me ..............
# 2  
Old 05-13-2010
tail -fworks by closing and reopening the file.
there is no special magic involved.

you will have to do the same.
read the file.
save the position in the file with tell
close the file
sleep
then when you reopen it do a seek to the same spot.

this won't work of course if the top of the file has been trimmed in the interim.


perldoc -f seek
perldoc -f tell
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read several variables from command output via SSH

Hi Folks, I'm currently trying to read several values into different variables. Actually, what I'm doing works, but I get an error message. My attempts are: read strCPROC strIPROC strAPROC <<<$(ssh -n -T hscroot@$HMC "lshwres -r proc -m $strIDENT --level sys -F \"configurable_sys_proc_units... (11 Replies)
Discussion started by: NKaede
11 Replies

2. Solaris

How to read the output of snoop command?

Hi! I have run the following command: snoop -q -d e1000g0 -o /var/tmp/optima0.txt & them I am trying to read the output of it with snoop -i /var/tmp/optima0.txt, which is giving me this: # snoop -i /var/tmp/optima0.txt | more 1 0.00000 AIOPTSVR -> 10.100.4.72 TCP D=1393 S=22 Push... (8 Replies)
Discussion started by: fretagi
8 Replies

3. Shell Programming and Scripting

Read 2 lines from File, Run Command based off output

Okay, so I have a file containing line after line of three digit numbers. I need a script that does an action based on the last two numbers in this list. So.... To get the last two numbers, I can have the script do tail -2 filename.txt But where I run into trouble is as follows. If... (6 Replies)
Discussion started by: UCCCC
6 Replies

4. UNIX for Advanced & Expert Users

ls output into a read command as a variable

I'm working on a short BASH script on my Ubuntu box that will run powerpoint scripts with MS Powerpoint Viewer 2007 via WINE. I can run the presentation when I run it manually but what i'd like to do is have the script look for the newest file then run it. #! /bin/sh # Start the newest... (2 Replies)
Discussion started by: binary-ninja
2 Replies

5. Shell Programming and Scripting

read line and run a different command according to the output

Hi. I'm trying to write a script that reads a line on a file and runs a different command for a different line output. For example, if it finds the word "Kuku" on the line it sends mail to Kuku@kuku.com. Otherwise, it sends mail to Lulu@lulu.com. TIA. (2 Replies)
Discussion started by: Doojek9
2 Replies

6. UNIX for Dummies Questions & Answers

read command - using output from command substitution

Hey, guys! Trying to research this is such a pain since the read command itself is a common word. Try searching "unix OR linux read command examples" or using the command substitution keyword. :eek: So, I wanted to use a command statement similar to the following. This is kinda taken... (2 Replies)
Discussion started by: ProGrammar
2 Replies

7. Shell Programming and Scripting

copy tail output to an external file in perl

The below code works to tail client.log file. but I want to copy the output into an external file /home/tail.txt Can anyone please help. #!/opt/bin/perl -w open my $pipe, "-|", "/usr/bin/tail", "/var/log/client.log" or die "could not start tail on /var/log/client.log : $!"; print while... (2 Replies)
Discussion started by: sureshcisco
2 Replies

8. UNIX for Advanced & Expert Users

How to alert if no output from tail -f command

Hi Dears, I am using AWK script to monitor some log files. My awk will get the input from the output of tail -f LOGFILE. (tail -f LOGFILE | myawkanalyzer) My doubt is, How can I notify, if I didn't get any output from the tail -f command. i.e) Log file stuck for some reason. I want to display... (1 Reply)
Discussion started by: sharif
1 Replies

9. Programming

How to read output of a shell command

Hello All, I have a an application written in C and runing on Red Hat Linux. In my code I have written a command that is fired on the linux shell by using system() function call. Now I need to read the output of this command in my c program and assign it to a variable. Can anyone... (1 Reply)
Discussion started by: shamik
1 Replies

10. UNIX for Advanced & Expert Users

Read the entire output fired by ps command

Actually I want to display the entire output fired by ps command. My output gets trucated after 80 chars. Thus, i am not able to see the entire command running when i give a ps -eaf .... Does anyone know how do i display the entire output fired by ps command .. (i.e the command along with... (5 Replies)
Discussion started by: vinithepoo
5 Replies
Login or Register to Ask a Question