Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

pid vs processId



  • what is the difference between pid and processId and which should i use to kill the process and how to kill it


  • Lifetime Qt Champion

    Hi,

    What are you talking ? QProcess ? If so, how do you use it ?
    If not, what are you doing ?
    What code are you using ?



  • @SGaist yes qprocess i start the servers in this function

    void ApplicationEngine::checkAndStartServers()
    {
    
        std::cout << "checking thrift" << std::endl;
        QProcess systemCheck;
        systemCheck.start("/bin/bash",QStringList() << "-c" << "ps -aux | grep \'[V]iBEDBi.py\' | wc -l");
        systemCheck.waitForFinished();
        QString output(systemCheck.readAllStandardOutput());
        int numInstants = output.toInt();
        if(numInstants < 1)
        {
          std::cout << "thrift has not started" << std::endl;
          std::cout << "starting thrift" << std::endl;
          thriftProcess.start("/bin/bash",QStringList() << "-c" << "cd /home/avidbeam/DB-management/ThriftDBi && python ViBEDBi.py");
          cout<<thriftProcess.pid();
        }
        else
        {
            std::cout << "Thrift already running\n";
            std::cout << "number of runnign thrift instants is: \n" << output.toStdString() << std::endl;
        }
    
        std::cout << "checking detection server" << std::endl;
        systemCheck.start("/bin/bash",QStringList() << "-c" << "ps -aux | grep \'[d]etection-server.json\' | wc -l");
        systemCheck.waitForFinished();
        output =QString(systemCheck.readAllStandardOutput());
        numInstants = output.toInt();
        if(numInstants < 1)
        {
          std::cout << "detection-server has not started: starting now ..." << std::endl;
          detectionServerProcess.start("/bin/bash",QStringList() << "-c" << "cd /home/avidbeam/workspace/ATUN_plugins/Phoenix_1_1_0/avidauto-plugin/submodules/detector && /usr/local/lib/ViBESDK_exe -j detection-server.json -v 3");
        }
        else
        {
            std::cout << "detection-server already started: \n" << output.toStdString() << std::endl;
        }
    
        std::cout << "checking recognition server" << std::endl;
        systemCheck.start("/bin/bash",QStringList() << "-c" << "ps -aux | grep \'[r]ecognition-server.json\' | wc -l");
        systemCheck.waitForFinished();
        output =QString(systemCheck.readAllStandardOutput());
        numInstants = output.toInt();
        if(numInstants < 1)
        {
          std::cout << "recognition-server has not started: starting now ..." << std::endl;
          recognitionServerProcess.start("/bin/bash",QStringList() << "-c" << "cd ~/workspace/ATUN_plugins/Phoenix_1_1_0/avidauto-plugin/submodules/ocr && /usr/local/lib/ViBESDK_exe -j recognition-server.json -v 3");
        }
        else
        {
            std::cout << "recognition-server already started: \n" << output.toStdString() << std::endl;
        }
    
    }
    

    and after i finish i want to create a function to check if those servers stoped by checking if their pid is 0 or not and if not i want to kill them



  • @Dijkstra
    A couple of observations:

    • You start your second ps too soon after you have spawned your detectionServerProcess. Since you don't wait, there is certainly a chance your _exe will not have started yet.

    • I would not be running bash/ps/grep/wc processes. I would be looking in the Linux /proc vfs.

    • If you really want kill you'll want to run kill, or you might go for pkill and save on half this stuff. Or you might call kill() function directly (man kill(2)).

    • You might look at have your shell script get the pid of the process created and return it, instead of all this ps looking up.

    • You might be better moving more/all of the logic into the shell script (e.g. supply a shell script file with your app) instead of doing half of it here in your C++ code, and hard-coded at that. It also may make it easier to adapt if there are problems/changes/enhancements required, rather than have to alter your C++ code.

    There is also QProcess::terminate/kill(), though they might not work well with the way you are spawning. If your _exe is a "server" (Linux demon) you probably ought be using QProcess::startDetached() rather than start().



  • @JonB so checking the pid then using .kill() is not going to work or what

    if(thriftProcess.pid()){
            thriftProcess.kill();
        }
        if(detectionServerProcess.pid()){
            detectionServerProcess.kill();
        }
        if(recognitionServerProcess.pid())
            recognitionServerProcess.kill();
    


  • @Dijkstra
    Depends on how your various executables behave, and bash. Try this i.e. (kill(1), and you may or may not have to go kill -9 ...) from a terminal/shell outside of Qt.



  • @JonB
    what about this

    QProcess systemcheck,kill;
        systemcheck.start("/bin/bash",QStringList() << "-c" << "ps -aux | grep chrome | awk '{print $2}'");
        systemcheck.waitForFinished();
        QString output(systemcheck.readAllStandardOutput());
        QStringList pids=output.split('\n');
        pids.pop_back();
        //cout<<output.toStdString()<<endl;
        if(pids.size()){
            for(auto it:pids){
                kill.start(QString("kill -9 %1").arg(it.toInt()));
                cout<<it.toInt()<<endl;
            }
        }
    

    i get the pid's of the process i am searching for and adding them to a list and running the kill command on them i really dont know what i am supposed to do after i run this it outputs this

    535
    QProcess::start: Process is already running
    556
    QProcess::start: Process is already running
    676
    QProcess::start: Process is already running
    678
    QProcess::start: Process is already running
    5690
    QProcess::start: Process is already running
    5700
    QProcess::start: Process is already running
    5701
    QProcess::start: Process is already running
    5702
    QProcess::start: Process is already running
    5705
    QProcess::start: Process is already running
    5726
    QProcess::start: Process is already running
    5729
    QProcess::start: Process is already running
    5742
    QProcess::start: Process is already running
    5789
    QProcess::start: Process is already running
    5812
    QProcess::start: Process is already running
    5838
    QProcess::start: Process is already running
    6115
    QProcess::start: Process is already running
    22797
    QProcess::start: Process is already running
    31396
    QProcess::start: Process is already running
    31834
    QProcess::start: Process is already running
    32434
    QProcess: Destroyed while process ("kill") is still running.```

  • Lifetime Qt Champion

    @Dijkstra said in pid vs processId:

    kill.start(QString("kill -9 %1").arg(it.toInt()));

    This is wrong. Use the static https://doc.qt.io/qt-5/qprocess.html#execute method...
    Also, parameters are passed as string list as described in the documentation.



  • @Dijkstra
    I'll say it again, and then leave to you: for the complexity/number of calls/amount of hard-coding you're doing, I would move all this logic into external script files. Up to you.



  • @jsulm

    if(pids.size()){
            for(auto it:pids){
                QProcess::execute("kill -9 ",QStringList()<<it);
                cout<<it.toInt()<<endl;
            }
        }
    

    like this ?



  • @Dijkstra
    No, because kill -9 is not a command! QProcess::execute("kill", QStringList()<< "-9" << it);



  • @JonB it worked thank you


Log in to reply