1

I am trying to kill a process that I have forked off for the purpose of running a python script that I have installed. This script can be killed via crtl-c when running in the codeblocks terminal and when being run from the command line (bash).

I have tried SIGINT and SIGQUIT. When I do not run the system call to python it works ok.

int main()
{
    int yahooTickerGrabberServicePID = fork();
    if (yahooTickerGrabberServicePID == 0)
    {
        system("YahooTickerDownloader.py");
        return EXIT_SUCCESS;
    }
    // sleep for testing only.
    sleep(5);
// Check to see if child is still running - 0 return indicates this is still running because the command can be sent...
        if(kill(yahooTickerGrabberServicePID, 0) == 0)
        {
            printf("good - still running...");
        }
        printf("sending sigint...");
        kill(yahooTickerGrabberServicePID, SIGINT);
        waitpid(yahooTickerGrabberServicePID, NULL, 0);
        while(processStillRunning == 0)
        {
            // printf("still running... :( ");
            kill(yahooTickerGrabberServicePID, SIGINT);
            processStillRunning = kill(yahooTickerGrabberServicePID, 0);
            waitpid(yahooTickerGrabberServicePID, NULL, 0);
        }
        printf("stopped?!");
        return EXIT_SUCCESS;
    }
}

I am aware I need more checks on the returns above, this is a full MWE.

The above does not kill the script - how can I make sure that SIGINT gets through to the python running from the system call, as it does when I hit it from the keyboard?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • You need to make up your mind first. Do you want to use `SIGINT` or `SIGQUIT`. You keep saying one, but the code sends the other. – Sam Varshavchik Aug 04 '19 at 15:07
  • Sorry - I need to use SIGINT. I presume SIGQUIT was more reliable, so investigated with using that to try to learn more about the issue. Fixed! –  Aug 04 '19 at 15:20
  • There are a couple of things you can do to analyze your problem. 1) Attempt to send a `SIGINT` to the process from the shell, using the shell `kill` command, to see if that terminates the process. 2) Attach `strace` to the python process. It's entirely possible that the process is ignoring the signal. You can use SIGKILL to kill the process, SIGKILL cannot be ignored, but that's going to be rather rude. – Sam Varshavchik Aug 04 '19 at 15:28
  • I have tried from the terminal, no luck. I assumed that if the command worked by pressing ctrl-c in the terminal (either one), it meant that it was in fact my implementation that was stopping it working. Could it be that python handles user-input SIGINT and remote SIGINT differently? I am running codeblocks as root as well. I will look into strace! Thank you. SIGKILL does work, but I cant afford to be this rude in this case. –  Aug 04 '19 at 16:44
  • There's only one kind of SIGINT. – Sam Varshavchik Aug 04 '19 at 18:12

0 Answers0