Detect background processing

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????
brent.websterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gareeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.