how can a process detect it is running in background? - Linux

This is a discussion on how can a process detect it is running in background? - Linux ; I have some shell experience, but I cannot figure out how to do this on Linux. I have scripts that have interactive parts that I would like to run in the background and have the script ignore the interactive parts. ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: how can a process detect it is running in background?

  1. how can a process detect it is running in background?

    I have some shell experience, but I cannot figure out how to do this on
    Linux.

    I have scripts that have interactive parts that I would like to run
    in the background and have the script ignore the interactive parts.

    For example, I want to:

    ......

    AM_I_ON_A_REAL_TERMINAL=`this part is what I don't know how to do`

    if [ $AM_I_ON_A_REAL_TERMINAL = "YES" ]
    then
    vi $MY_REPORT
    else
    mail -s"Here's your report" me@mymachine.com < $MY_REPORT
    fi
    ........

    It is trivial on SCO, but escapes me on Linux.
    SCO:
    # tty
    /dev/ttyp4
    # tty &
    not a tty
    #

    but on Linux:
    # tty
    /dev/pts/26
    # tty &
    /dev/pts/26
    #

    Fortunately, on Linux
    at now + 2 minutes
    tty > /tmp/mytty
    ^d

    wait 2 minutes,
    # cat /tmp/mytty
    not a tty

    So that works fine - it's just frustrating to not know how to do this
    "in the background".

    Feeling dumb, but it's not the end of the world.


  2. Re: how can a process detect it is running in background?

    emily wrote:
    > I have some shell experience, but I cannot figure out how to do this on
    > Linux.
    >
    > I have scripts that have interactive parts that I would like to run
    > in the background and have the script ignore the interactive parts.
    >
    > For example, I want to:
    >
    > .....
    >
    > AM_I_ON_A_REAL_TERMINAL=`this part is what I don't know how to do`


    man test

    Here the test '-t': "file descriptor FD (stdout by default) is opened on
    a terminal" might be of help.

    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett


  3. Re: how can a process detect it is running in background?

    emily wrote:

    ......

    >
    > AM_I_ON_A_REAL_TERMINAL=`this part is what I don't know how to do`
    >
    > if [ $AM_I_ON_A_REAL_TERMINAL = "YES" ]
    > then
    > vi $MY_REPORT
    > else
    > mail -s"Here's your report" me@mymachine.com < $MY_REPORT
    > fi
    > .......
    >
    > It is trivial on SCO, but escapes me on Linux.
    > SCO:
    > # tty
    > /dev/ttyp4
    > # tty &
    > not a tty
    > #
    >
    > but on Linux:
    > # tty
    > /dev/pts/26
    > # tty &
    > /dev/pts/26
    > #
    >
    > Fortunately, on Linux
    > at now + 2 minutes
    > tty > /tmp/mytty
    > ^d
    >
    > wait 2 minutes,
    > # cat /tmp/mytty
    > not a tty
    >
    > So that works fine - it's just frustrating to not know how to do this
    > "in the background".
    >
    > Feeling dumb, but it's not the end of the world.



    Thank you very much.
    Unfortunatly, neither of those suggestions seem to execute
    differently interactively versus 'in the background' on the
    Linux versions I have tested (RedHat 7.3, Suse 9.3).

    I'm beginning to believe it can't be done in Linux, and I can
    live with that.

    Again, since I'm able to detect when the process is cronned,
    I'll just put up with not being able to test how it would operate
    in 'cronned' mode by executing it in 'background'.

    Again, thanks for your help.


  4. Re: how can a process detect it is running in background?

    emily wrote:
    > emily wrote:
    >
    > .....
    >
    >> AM_I_ON_A_REAL_TERMINAL=`this part is what I don't know how to do`
    >>
    >> if [ $AM_I_ON_A_REAL_TERMINAL = "YES" ]
    >> then
    >> vi $MY_REPORT
    >> else
    >> mail -s"Here's your report" me@mymachine.com < $MY_REPORT
    >> fi
    >> .......
    >>
    >> It is trivial on SCO, but escapes me on Linux.
    >> SCO:
    >> # tty
    >> /dev/ttyp4
    >> # tty &
    >> not a tty
    >> #
    >>
    >> but on Linux:
    >> # tty
    >> /dev/pts/26
    >> # tty &
    >> /dev/pts/26
    >> #
    >>
    >> Fortunately, on Linux
    >> at now + 2 minutes
    >> tty > /tmp/mytty
    >> ^d
    >>
    >> wait 2 minutes,
    >> # cat /tmp/mytty
    >> not a tty
    >>
    >> So that works fine - it's just frustrating to not know how to do this
    >> "in the background".
    >>
    >> Feeling dumb, but it's not the end of the world.

    >
    >
    > Thank you very much.
    > Unfortunatly, neither of those suggestions seem to execute
    > differently interactively versus 'in the background' on the
    > Linux versions I have tested (RedHat 7.3, Suse 9.3).
    >
    > I'm beginning to believe it can't be done in Linux, and I can
    > live with that.
    >
    > Again, since I'm able to detect when the process is cronned,
    > I'll just put up with not being able to test how it would operate
    > in 'cronned' mode by executing it in 'background'.
    >
    > Again, thanks for your help.
    >


    To test the cron behavior, redirect input from /dev/null

    tty
    Also make sure you're not relying on environment variables that may
    not be set within cron. Note that "at" captures your env vars and
    uses them for execution whereas cron has a stock of vars it uses.

  5. Re: how can a process detect it is running in background?

    Josef Moellers wrote in part:
    > emily wrote:
    >> I have some shell experience, but I cannot figure out how to do this on
    >> Linux.
    >>
    >> I have scripts that have interactive parts that I would like to run
    >> in the background and have the script ignore the interactive parts.
    >>
    >> For example, I want to:
    >>
    >> .....
    >>
    >> AM_I_ON_A_REAL_TERMINAL=`this part is what I don't know how to do`

    >
    > man test
    >
    > Here the test '-t': "file descriptor FD (stdout by default)
    > is opened on a terminal" might be of help.



    Blasphemy The Way of Unix, and other Linux-like systems
    is to strive for flexibility and maximum device-independance.
    Processes ought to be kept in the dark!

    Where one pgm is to have multiple modes (vaguely adulterous), then
    they ought to be activated by command-line options. The alerts
    the less ignorant users that the pgm is running differently.
    For example `fsck -y` to mangle filesystems automagically.

    -- Robert



+ Reply to Thread