about how Linux background running process masks CTRL + Z

Source: Internet
Author: User

Recently encountered shell background boot process, input CTRL + Z is suspended problem

Described below:

1, existing two shell scripts, grandfather.sh and father.sh, two C + + programs Son1 and Son2, where Son1 is a normal program,son2 use Occi to connect to Oracle database

The call relationship is


2, father.sh start Son1 and Son2, exit to grandfather.sh, at this time in the grandfather.sh Ctrl + Z, use PS aux to view the process status of Son1 and Son2 respectively T and S

3. Personal opinion, Since father.sh in the background to start Son1 and Son2, then Son1 and son2 should not be affected by the foreground shell, no longer receive the foreground shell signal, not to mention Father.sh has exited, Son1 and Son2 parent process PID has been 1. But in fact, grandfather.sh and Son1 and Son2 still have inheritance relationship, share some resources and signals, cause son1 and Son2 will receive CTRL + Z signal, so Son1 is suspended, At this time kill off grandfather.sh, Son1 Normal, explain Son1 and grandfather.sh indeed have association.

4, for Son2, why has not been suspended? The reason is that OCCI will capture a signal such as CTRL + Z, so the surface son2 is not affected, but it may actually connection have been rolled back or gone wrong.

5, the solution is divided into two, one is in the C + + program to mask the signal, and the second is to shield the signal in the shell script

6, in C + + program shielding CTRL + Z signal

    Signum is a digital representation of the signal, wherein Sigstop=ctrl+c,sigtstp=ctrl+z    int32_t Signum;    Callback is the callback function Signalcallback callback when the signal responds    ;    struct Sigaction Act, oldact;    memset (&act, 0, sizeof (ACT));    memset (&oldact, 0, sizeof (oldact));    Sigemptyset (&act.sa_mask);    Act.sa_handler = callback;    Return Sigaction (Signum, &act, &oldact);
However, Occi will take precedence over C + + program capture signals, so this method does not work for son2 programs

7. Block CTRL + Z signals in shell scripts

Trap Exit Sigusr2trap Exit Sigusr1trap exit Sigtermtrap exit Sigstoptrap exit Sigttintrap exit Sigtstptrap exit Sigttintra P Exit Sigttoutrap exit Sigchldtrap exit SIGINT
The trap command requires two parameters, the second parameter is the number of the signal, the first parameter is to receive the signal, to replace the action, if it is only a shielding signal, you can set the first parameter to "

For background operation, please refer to the article http://www.cnblogs.com/SuperXJ/archive/2011/10/31/2230314.html

about how Linux background running process masks CTRL + Z

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.