簡體   English   中英

C ++ Linux從程序執行命令行

[英]c++ linux execute command line from program

我正在尋找一種方法來從我的C ++程序調用特定的命令行調用。 該命令需要多次調用。 然后從用戶輸入中取消。

程序流程如下:

  • 啟動程序等待用戶按下某個按鈕。
  • 按下時,需要觸發命令。

     bmdcapture -C 0 -m 2 -F nut -m 11 -V 3 -A 2 -f pipe:1 | avconv -y -i - -strict experimental -q:v 1 -c:v mpeg4 '/var/www/recvideos/mytest0.mp4' 
  • 然后等待用戶按下相同的按鈕並終止上述命令。

我有按下按鈕的邏輯,但是在不接管程序的情況下無法找到一種執行命令的方法。 我看過fork(),但不確定它是否可以與按鈕按下邏輯一起使用。

任何幫助,將不勝感激

這是按鍵觀察者,在撰寫本文時,我正在玩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);
}
}

如果將&符號附加到命令中,然后使用system()執行命令,則程序將繼續運行,而命令在后台同時運行。 例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM