APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

How to tell if script is running in background


© December 2004 Tony Lawrence

The method given below will work under some conditions, but I'd use "tty", which will return "not a tty" if in background.

The usual reason for wanting to know is to shut up output to avoid wasting time and possibly filling up a log file.

Here is an answer for Perl.

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
Message-ID: <925208173.823.10@news.remarQ.com> 

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   https://www.armory.com./~spcecdt/


Got something to add? Send me email.





(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

->
-> How to tell if script is running in background ––>Re:detecting background process


Inexpensive and informative Apple related e-books:

iOS 8: A Take Control Crash Course

Are Your Bits Flipped?

Take Control of iCloud, Fifth Edition

Take Control of iCloud

Take Control of High Sierra




More Articles by © Tony Lawrence




Printer Friendly Version

Have you tried Searching this site?

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.

Contact us


Printer Friendly Version





We made the buttons on the screen look so good you'll want to lick them. (Steve Jobs)




Linux posts

Troubleshooting posts


This post tagged:

Scripting



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode





SCO Unix Sales, Support, & Service

Phone:  707-SCO-UNIX (707-726-8649Toll Free: 833-SCO-UNIX (833-726-8649)
www.SCOsales.com