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 http://www.armory.com./~spcecdt/
More Articles by Tony Lawrence - Find me on Google+
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.
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.
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