I've tried to find an answer to this problem but It seems like there is something which I do not quite understand:

I have a script in which dumps a lot of output to the shell. When I do use the ampersand (&) to run the script as a background process it still prints the same output to the shell. So my questions is: Is it possible for the script to know that it has been put in the background by ampersand so that it can silence itself (re-direct STDOUT/STDERR TO /dev/null)?

Update: Thanks everyone for great feedback. I got what I was looking for and I ended up with a check that was in almuts link:

171 if (!open(TTY, "/dev/tty")) { 172 print "no tty\n"; 173 } else { 174 my $tpgrp = tcgetpgrp(fileno(*TTY)); 175 my $pgrp = getpgrp(); 176 if ($tpgrp == $pgrp) { 177 print "foreground\n"; 178 } else { 179 print "background\n"; 180 } 181 }

In reply to How to know if a perl script is put in the background by rapide

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.