469,336 Members | 5,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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

15
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.
Feb 16 '07 #1
11 6298
Natti
15
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.
Feb 17 '07 #2
KevinADC
4,059 Expert 2GB
open(FILE, "tail -F -n 10 $file |") or die;
Feb 17 '07 #3
Natti
15
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.
Feb 17 '07 #4
miller
1,089 Expert 1GB
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
Feb 17 '07 #5
Natti
15
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.
Feb 17 '07 #6
miller
1,089 Expert 1GB
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.  
Feb 17 '07 #7
Natti
15
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
Feb 18 '07 #8
@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
Feb 19 '07 #9
Natti
15
@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
Feb 22 '07 #10
@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
Feb 22 '07 #11
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...
May 1 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

134 posts views Thread by James A. Donald | last post: by
2 posts views Thread by news.bellatlantic.net | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.