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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Natti
    New Member
    • Feb 2007
    • 15

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

    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.
  • Natti
    New Member
    • Feb 2007
    • 15

    #2
    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.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      open(FILE, "tail -F -n 10 $file |") or die;

      Comment

      • Natti
        New Member
        • Feb 2007
        • 15

        #4
        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.

        Comment

        • miller
          Recognized Expert Top Contributor
          • Oct 2006
          • 1086

          #5
          Originally posted by Natti
          As a small example,

          I open my file using
          Code:
          #!/usr/bin/perl -w
          [b]open(FILE, "tail -F -n 10 $file |") or die "Error $!\n";[/b]
          '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.

          Comment

          • Natti
            New Member
            • Feb 2007
            • 15

            #6
            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.

            Comment

            • miller
              Recognized Expert Top Contributor
              • Oct 2006
              • 1086

              #7
              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:



              Code:
              #!/usr/bin/perl
              use strict;
              my $file = "./test.txt"
              [b]if (-e $file) {[/b]
              	open(FILE, "tail -F -n 10 $file") or die;
              }

              Comment

              • Natti
                New Member
                • Feb 2007
                • 15

                #8
                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:



                Code:
                #!/usr/bin/perl
                use strict;
                my $file = "./test.txt"
                [b]if (-e $file) {[/b]
                	open(FILE, "tail -F -n 10 $file") or die;
                }

                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

                Comment

                • docsnyder
                  New Member
                  • Dec 2006
                  • 88

                  #9
                  @Natti

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

                  Depending on your Shell, this should look like:
                  Code:
                  open(FILE, "tail -F -n 10 $file 2>& /dev/null") or die;
                  Greetz, Doc

                  Comment

                  • Natti
                    New Member
                    • Feb 2007
                    • 15

                    #10
                    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:
                    Code:
                    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

                    Comment

                    • docsnyder
                      New Member
                      • Dec 2006
                      • 88

                      #11
                      @Natti

                      You could, as a workaround, redirect STDERR to STDOUT.
                      Code:
                      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

                      Comment

                      • crazychickenhead
                        New Member
                        • May 2007
                        • 1

                        #12
                        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...

                        Comment

                        Working...