Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Detect background processing

Posted on 1997-04-22
1
Medium Priority
?
426 Views
Last Modified: 2006-11-17
I.E. Within my perl script, if I'm running as a foreground
process then I want to give the user the option to alter the setup. If the script is run as a background process then it runs with defaults set.

I've had numerous suggestions from checking the $SHELL env var to the -t test operator to system("tty -s") and checking $? var.  These can all distingush between running as a foreground process or as a cron/at process but they don't work for background process spawn via nohup or & from a foreground shell process. i.e. they inherit from the parent shell.

Any ideas????
0
Comment
Question by:brent.webster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

by:
garee earned 300 total points
ID: 1203865
Here's a small C code which does what you want.  It can be
converted to Perl.  The ioctl may be tricky in Perl, I haven't
looked into it.

/* This program checks to see if the process is running in the background.
   The process is in the background if its standard output is not directed
   to a tty, or if the terminal output device's process group is not the same
   as the process's process group.

   Usage:  isbackground [-v]

            -v  Report findings
   
   Return value:  0  process is in background
                  1  process is in foreground
                  2  error  */

#include <sys/ioctl.h>

main(int argc, char **argv)
{
   int  tty;
   int  pgrp, tpgrp;
   int  vflag = 0;

   if (argc > 1)
   {
      vflag = (strcmp(argv[1], "-v") == 0);
   }

   if (isatty(1))
   {
      if (ioctl(1, TIOCGPGRP, &tpgrp))
      {
         perror("ioctl");
         exit(2);
      }

      pgrp = getpgrp();

      if (pgrp == tpgrp)
      {
         if (vflag)  printf("Process running in foreground.\n");
         exit(1);
      }
      else
      {
         if (vflag)  printf("Process running in background.\n");
         exit(0);
      }
   }

   if (vflag)  printf("Process running in background.\n");
   exit(0);
}

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question