user3740847
user3740847

Reputation: 1

c++ linux execute command line from program

I'm looking for a method to call a specific command line call from my c++ program. This command need to be called mulitple times async. and then be canceled from user input.

Program flow is as follows:

I have the logic for the button press, but cant find a way to execute the command without it taking over the program. I have looked at fork(), but I'm uncertain it will work with the button press logic.

Any help would be appreciated

Here is the keypress watcher and at the time of this post I was playing with pthread.

while(1)
{
if (kbhit()) {
    int keypressed = getch();
    //printw("End Capture Key pressed! It was: %d\n", keypressed);
    printw("Capture will begin again when ctrl+r is pressed. \n");
    if(keypressed == 18)
    {
        //exit capture logic here
        int j;
        for(j=0; j < 1; j++)
        {
            pthread_cancel(threads[j]);
        }
        captureActive = false;
        break;
    }
}
else if(!captureActive)
{
    printw("Starting Capture...\n");
    //Begin Capture on all active cards
    int rc;
    int i;
    for(i=0; i < 1; i++)
    {
        rc = pthread_create(&threads[i], NULL, Capture, (void*)&threads[i]);

        if(rc)
        {
            cout << "ERROR: unable to create thread: " << rc << endl;
            exit(-1);
        }
    }

    captureActive = true;
}
else {
    //printw("capturing...\n");
    refresh();
    usleep(1000000);
}
}

Upvotes: 0

Views: 633

Answers (1)

mti2935
mti2935

Reputation: 12037

If you append the & symbol to your command, then execute your command using system(), then your program will continue to run, while your command runs simultaneously in the background. For example:

sprint(cmd, "/path/to/your/command &");
system(cmd);

Upvotes: 2

Related Questions