Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. pid vs processId
Qt 6.11 is out! See what's new in the release blog

pid vs processId

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS SGaist

    Hi,

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

    D Offline
    D Offline
    Dijkstra
    wrote on last edited by
    #3

    @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

    JonBJ 1 Reply Last reply
    0
    • D Dijkstra

      @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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #4

      @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().

      D 1 Reply Last reply
      2
      • JonBJ JonB

        @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().

        D Offline
        D Offline
        Dijkstra
        wrote on last edited by
        #5

        @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();
        
        JonBJ 1 Reply Last reply
        0
        • D Dijkstra

          @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();
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #6

          @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.

          D 1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            D Offline
            D Offline
            Dijkstra
            wrote on last edited by
            #7

            @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.```
            jsulmJ JonBJ 2 Replies Last reply
            0
            • D Dijkstra

              @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.```
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #8

              @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.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              D 1 Reply Last reply
              2
              • D Dijkstra

                @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.```
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #9

                @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.

                1 Reply Last reply
                2
                • jsulmJ jsulm

                  @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.

                  D Offline
                  D Offline
                  Dijkstra
                  wrote on last edited by
                  #10

                  @jsulm

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

                  like this ?

                  JonBJ 1 Reply Last reply
                  0
                  • D Dijkstra

                    @jsulm

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

                    like this ?

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #11

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

                    D 1 Reply Last reply
                    5
                    • JonBJ JonB

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

                      D Offline
                      D Offline
                      Dijkstra
                      wrote on last edited by
                      #12

                      @JonB it worked thank you

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved