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

Is Process Running In The Background?

Update: BTW, I'm a bit surprised that your backgrounded script's stdout/stderr is going to the terminal. Normally, it wouldn't, if you start a script from the shell using & without redirecting those handles. The shell would stop the backgrounded script in case it's writing anything to stdout or stderr....   Could you provide some sample code that shows what you're doing / allows to reproduce the issue?  Which OS, which shell?

Replies are listed 'Best First'.
Re^2: How to know if a perl script is put in the background
by mpeever (Friar) on Nov 18, 2008 at 17:48 UTC

    BTW, I'm a bit surprised that your backgrounded script's stdout/stderr is going to the terminal. Normally, it wouldn't, if you start a script from the shell using & without redirecting those handles. The shell would stop the backgrounded script in case it's writing anything to stdout or stderr....
    It's my experience that backgrounded jobs still write STDERR to the screen/terminal under every shell/OS combination. For example,
    tar cvf /dev/null /usr/local &
    fills my OS X terminal, as it does on Solaris and OpenBSD.

      ... fills my OS X terminal, as it does on Solaris and OpenBSD.

      Not here... (Linux / bash with default settings) — But I suppose this discussion is getting off-topic with respect to the original question.

      Update: Here's the respective paragraph from bash's man page:

      JOB CONTROL
      (...) Only foreground processes are allowed to read from or write to the terminal. Background processes which attempt to read from (write to) the terminal are sent a SIGTTIN (SIGTTOU) signal by the terminal driver, which, unless caught, suspends the process.

        So this really is controlled by stty, specifically the [-]tostop option. On my system (which doesn't stop background jobs that try to output), it's off ("-tostop"). I'm guessing that on your system it's on. Try:

        stty -a

        ...and see what it is.

Re^2: How to know if a perl script is put in the background
by rapide (Beadle) on Nov 18, 2008 at 16:55 UTC
    I created a small sample for you below. This behaves the same way ..
    OS: Linux 2.6.9-67.0.20 (SCL - Scientific Linux 4)
    Shell: bash
    Perl: 5.8.5
    1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 6 print "# Start script\n"; 7 print "\n -> Waiting 2 seconds\n"; 8 sleep(2); 9 print "\n -> Waiting 2 seconds\n"; 10 sleep(2); 11 print "# Exiting .. \n";

      Can't reproduce it, though I'm also using Linux and bash.  With your sample code I'm getting something like:

      $ ./724323.pl & [2] 10620

      (no further output)

      $ ps x | grep 724323 10620 pts/1 T 0:00 /usr/bin/perl ./724323.pl

      (the 'T' indicates that the script has been stopped)

      $ fg ./724323.pl # Start script -> Waiting 2 seconds -> Waiting 2 seconds # Exiting ..

      (after foregrounding it, output continues normally)

Re^2: How to know if a perl script is put in the background
by MidLifeXis (Monsignor) on Nov 18, 2008 at 17:41 UTC

    Depends on your shell. I run things in the background all of the time (tail /this/logfile&^Jtail /that/logfile &). As long as input is not required, they run just fine.

    --MidLifeXis