Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Detect background processing

Posted on 1997-04-22
1
Medium Priority
?
427 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
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

885 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