0

I have an exe file compiled from C++ code. And I use bash to set up Linux environment and to call this .exe programme.

Now coming to the problem, most of the time, users would like to use ctrlc to kill the processes when they finish using the programme or when they don't want to continue.

The bash process is terminated by ctrlc properly, however, the .exe is usually running without being killed. So users need to use kill -9 xxx to kill the process. If they forget to kill, their CPU could be fully occupied.

How shall I proceed to solve this problem ? Shall I do something for the code of exe file or for the bash script?

Thanks

Tanner
  • 22,205
  • 9
  • 65
  • 83
thundium
  • 995
  • 4
  • 12
  • 30
  • `As loog as they forget to kill, their CPU could be fully occupied` sounds like something is wrong with your code – hek2mgl Sep 12 '13 at 09:31
  • The exe is really heavy loaded. In top, I can see the CPU is 30% taken. But for the specific process, it shows 100% CPU. I guess since it is running on the sever, the whole CPUs are not occpupied. – thundium Sep 12 '13 at 09:36
  • 1
    `Shall I do something for the code of exe file or ...` -- Yes, do something about the code. Fix it. – devnull Sep 12 '13 at 09:37

3 Answers3

2
#!/bin/bash

./yourbinary &
pid=$!

trap "kill -9 $pid" SIGINT

wait
Aleks-Daniel Jakimenko-A.
  • 10,335
  • 3
  • 41
  • 39
0

This is from my old code written sometime long back. I had used this to kill ffmpeg, I am also pasting the code. I am not using ctr+c, I am using process pid to kill it. i think this must be the good way.

        else if (strcmp(client_message,"2-off") == 0) {
            printf("start audio streeaming will be turned of\n");
            fp = popen("pidof ffmpeg", "r");
            fgets(line, 100, fp);
            pid = atoi(line);
            printf("pid = %d\n",pid);
            sprintf(command,"kill %d",pid);
            status = system(command);
            pclose(fp);
        }
Megharaj
  • 1,589
  • 2
  • 20
  • 32
0

Do you detach your program in bash script? Do you call it like this:

exe&

You can delete "&" and script will wait for program end before executing next command.

If the problem is not there, try to attach your program to console. Example, in Qt project string:

CONFIG += console

in .pro file attaches application to console and script will wait for application end and terminate application with Ctrl+C.

Oleg Olivson
  • 489
  • 2
  • 5