Solved

Detect background processing

Posted on 1997-04-22
1
419 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 100 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

773 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