(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



How to tell if script is running in background


What is this stuff?

If this isn't exactly what you wanted, please try our Search (there's a LOT of techy and non-techy stuff here about Linux, Unix, Mac OS X and just computers in general!):



From - Tue Apr 27 07:47:45 1999
Xref: world comp.unix.sco.misc:95036
Path: world!newsswitch.lcs.mit.edu!remarQ-easT!remarQ73!supernews.com!remarQ.com!remarQ69!news.remarQ.com!not-for-mail
From: spcecdt@deeptht.armory.com. (John DuBois)
Newsgroups: comp.unix.sco.misc
Subject: Re: detecting background process
Date: Tue, 27 Apr 1999 10:16:13 GMT
Organization: The Armory
Lines: 46
Message-ID: <925208173.823.10@news.remarQ.com> 
References: <01be8c2d$45865100$21648b8e@GFACINI> <371e3f3c.198764031@news.tiac.net> <37621AF0.7DB8BDDA@ncs.co.nz> 
NNTP-Posting-Host: 192.122.209.42
NNTP-Posting-Date: Tue, 27 Apr 1999 10:16:13 GMT
X-Trace: 925208173.823.10 X59QMCGASD12AC07AC usenet1.supernews.com
X-Complaints-To: newsabuse@remarQ.com
X-Newsreader: trn 4.0-test69 (20 September 1998)
X-Mozilla-Status: 8011

In article <37621AF0.7DB8BDDA@ncs.co.nz>, Greg M Lee  <greg@ncs.co.nz> wrote:
>I use:
>#!/bin/sh
>if [ -t 0 ];then
>        echo "running in foreground"
>else
>        echo "running in background"
>fi
>
>Enjoy
>-Greg
>
>Art L. wrote:
>> 
>> I am sure there is a better way, but a method I use is to look at the
>> return of the tty command.
>> 
>> #!/bin/sh
>> if [ '`tty`' = "not a tty" ];then
>>         echo "running in background"
>> else
>>         echo "running in foreground"
>> fi
>> 
>> On 21 Apr 1999 19:29:28 GMT, "Gino Facini" <gino.facini@gov.nb.ca>
>> wrote:
>> 
>> >I'm fairly new to Unix scripting and I need to know if the script that is
>> >running was started in the background or not.  If anyone has an easy way of
>> >telling this could you please email me personally at gino.facini@gov.nb.ca
>> >

Are you using sh or ksh?  If sh, the above solutions will work.  If ksh (or
another job-control shell), the handling of background jobs is more refined
with the result that the above won't work.  This method will work with both:



if {</dev/tty;} 2>/dev/null; then
    echo "running in foreground"
else
    echo "running in background"
fi


        John
-- 
John DuBois    spcecdt@armory.com.    KC6QKZ   http://www.armory.com./~spcecdt/




Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar



/Bofcusm/41.html copyright 1997-2004 (various authors) All Rights Reserved

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.



More:
       - OSR5
       - Bofcusm


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress