Connecting Tech Pros Worldwide Forums | Help | Site Map

Hiding tail "no such file or directory" message in Perl/Tk

Newbie
 
Join Date: Feb 2007
Posts: 15
#1: Feb 16 '07
Hello,
I start fileevents as soon as my tk code is invoked. My widget gets updated based on the fileevents. Most of the files do not exist in the beginning and so I get the following messages on the prompt where I invoked the perl/tk program.

tail 'filename' for reading:no such file or directory.

The widget update works fine. But is there anyway that I can prevent these messages from showing up. Also when information starts streaming into these files, the message below shows up,

tail: 'filename' has appeared; following end of new file

I would like to avoid these kinds of messages while executing my code.

How can I do this.

Newbie
 
Join Date: Feb 2007
Posts: 15
#2: Feb 17 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


As a small example,

I open my file using

#!/usr/bin/perl -w
open(FILE, "tail -F -n 10 $file |") or die "Error $!\n";

'tail cannot open <filename> no such file or directory'

is written out to my shell.

How can I hide this.
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Feb 17 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


open(FILE, "tail -F -n 10 $file |") or die;
Newbie
 
Join Date: Feb 2007
Posts: 15
#4: Feb 17 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


That doesnt work either. I still get the message on my shell.

The message does not come from the Error $! but gets dumped out when perl tries to execute tail to open the file.
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#5: Feb 17 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


Quote:

Originally Posted by Natti

As a small example,

I open my file using

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. open(FILE, "tail -F -n 10 $file |") or die "Error $!\n";
  3.  
'tail cannot open <filename> no such file or directory'

is written out to my shell.

How can I hide this.

In the above example, "$file" is never assigned a value. So essentially you're calling "tail -F -n 10 |" which while legal is not useful as it returns the above error. Try assigning a value to '$file', and alternatively, try using the cpan module for tailing files.

http://search.cpan.org/search?query=File::Tail
Newbie
 
Join Date: Feb 2007
Posts: 15
#6: Feb 17 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


Quote:

Originally Posted by miller

In the above example, "$file" is never assigned a value. So essentially you're calling "tail -F -n 10 |" which while legal is not useful as it returns the above error. Try assigning a value to '$file', and alternatively, try using the cpan module for tailing files.

http://search.cpan.org/search?query=File::Tail

Hello Miller,
I just put in $file as an example. In my code I assign it a value. Anyway if the file does not exist, the tail warning comes up. As an example
Assume my file ./test.txt does not exist


#!/usr/bin/perl
$file = "./test.txt"
open(FILE, "tail -F -n 10 $file") or die;

gives out the tail warning saying no file or directory.
Once I cat some value into the file ./text.txt the tail handler gets updated and works fine.
It is the initial tail messages that I want to avoid popping up on my screen.
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#7: Feb 17 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


Hi Natti,

Then quite simply, add logic that will ensure that the file exists first before calling tail. Read about all the special file tests here:

http://perldoc.perl.org/functions/-X.html

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. my $file = "./test.txt"
  4. if (-e $file) {
  5.     open(FILE, "tail -F -n 10 $file") or die;
  6. }
  7.  
Newbie
 
Join Date: Feb 2007
Posts: 15
#8: Feb 18 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


Quote:

Originally Posted by miller

Hi Natti,

Then quite simply, add logic that will ensure that the file exists first before calling tail. Read about all the special file tests here:

http://perldoc.perl.org/functions/-X.html

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. my $file = "./test.txt"
  4. if (-e $file) {
  5.     open(FILE, "tail -F -n 10 $file") or die;
  6. }
  7.  


Hello Miller,
Thanks for the info on this post. I tried that already. However, I go into the subroutine and start the tail process for a list of files at the beginning of execution. That prevents me from using the -e option.
I managed to overcome this problem by making sure that the file exists before the subroutine is called. i.e. I will cat a null value into the file if it does not exist.

Natti
docsnyder's Avatar
Member
 
Join Date: Dec 2006
Location: Darmstadt
Posts: 88
#9: Feb 19 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


@Natti

Try to redirect STDERR! (this is where I assume your messages are printed to)

Depending on your Shell, this should look like:
Expand|Select|Wrap|Line Numbers
  1. open(FILE, "tail -F -n 10 $file 2>& /dev/null") or die;
Greetz, Doc
Newbie
 
Join Date: Feb 2007
Posts: 15
#10: Feb 22 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


Quote:

Originally Posted by docsnyder

@Natti

Try to redirect STDERR! (this is where I assume your messages are printed to)

Depending on your Shell, this should look like:

Expand|Select|Wrap|Line Numbers
  1. open(FILE, "tail -F -n 10 $file 2>& /dev/null") or die;
Greetz, Doc


docsnyder,
I am open a pipe to the tail and then using the fileevent. The redirection to /dev/null does not work and I get the following error message.

sh: /dev/null: ambiguous redirect
docsnyder's Avatar
Member
 
Join Date: Dec 2006
Location: Darmstadt
Posts: 88
#11: Feb 22 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


@Natti

You could, as a workaround, redirect STDERR to STDOUT.
Expand|Select|Wrap|Line Numbers
  1. open(FILE, "tail -F -n 10 $file 2>&1 |") or die "$!";
This would force error messages to be channeled through the pipe as well.

When reading from the pipe, the error messages could simply be skipped.

Not nice, of course, but serves your needs.

Greetz, Doc
Newbie
 
Join Date: May 2007
Posts: 1
#12: May 1 '07

re: Hiding tail "no such file or directory" message in Perl/Tk


Redirect stderr to /dev/null instead of another descriptor, like this:

2>/dev/null

I use this when I want stdin/stdout but want to ignore errors.

#!/usr/bin/perl
`rm file.notexist 2>/dev/null`;
`cat part1.txt >> whole.txt 2>/dev/null`;

Hope that helps...
Reply