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. Run command line from Qt app in linux
Forum Updated to NodeBB v4.3 + New Features

Run command line from Qt app in linux

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 5 Posters 12.0k Views 3 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.
  • G Offline
    G Offline
    grullo
    wrote on last edited by
    #3

    Thank you SGaist for your quick reply

    what you coment I tried, in fact, I tried

    using namespace std;
    cout<<"checking the use of ls in qt * \n";
    QProcess process;
    process.setWorkingDirectory("/home/grullo");
    process.start("ls", QStringList() << "-l");
    process.waitForFinished();
    QString StdOut = process.readAllStandardOutput(); //Reads standard output
    cout<<endl<<StdOut.toStdString();

    But still the output is empty, my main worry is that the first cout is not printed. And in that point the code cannot be failing

    Thanks in advance

    mrjjM 1 Reply Last reply
    0
    • G grullo

      Thank you SGaist for your quick reply

      what you coment I tried, in fact, I tried

      using namespace std;
      cout<<"checking the use of ls in qt * \n";
      QProcess process;
      process.setWorkingDirectory("/home/grullo");
      process.start("ls", QStringList() << "-l");
      process.waitForFinished();
      QString StdOut = process.readAllStandardOutput(); //Reads standard output
      cout<<endl<<StdOut.toStdString();

      But still the output is empty, my main worry is that the first cout is not printed. And in that point the code cannot be failing

      Thanks in advance

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #4

      @grullo
      Hi
      Your code works fine for me if i change the home folder
      Try to also include the StdError like in the sample

      https://gist.github.com/technoburst/5369746

      and see if it gives an error.

      1 Reply Last reply
      2
      • G Offline
        G Offline
        grullo
        wrote on last edited by
        #5

        Thanks @mrjj

        I'm not sure why... but now it is working. I introduced some modifications from the example you posted. At least I had a mistake in the path of the working directory.

        The last thing related with this issue. When I read the standard output, the process (problem I run in the shell) must finish. The ls command is very fast, but the script I need to run is not ls, and the script can take some hours. I would like to show in real time the standard output to the users, instead of waiting until the end of the process. Is this possible?

        Many thanks for all your help!

        I copy here the final code, perhaps someone else have my problem

        void MainWindow::on_browseParticles_clicked()
        {
        using namespace std;
        cout<<"checking the use of ls in qt * \n";
        QProcess process;
        process.setWorkingDirectory("/home/grullo");

        QString Command;    //Contains the command to be executed
        QStringList args;   //Contains arguments of the command
        
        Command = "ls";
        args<<"-l"<<"/home/vilas";
        
        process.start(Command, args, QIODevice::ReadOnly); //Starts execution of command
        
        process.waitForFinished();
        QString StdOut = process.readAllStandardOutput(); //Reads standard output
        cout<<"\n Printing the standard output..........\n";
        cout<<endl<<StdOut.toStdString();
        
        cout<<"\n Printing the standard error..........\n";
        QString StdError    =   process.readAllStandardError();   //Reads standard error
        cout<<endl<<StdError.toStdString();
        

        }

        JonBJ 1 Reply Last reply
        0
        • G grullo

          Thanks @mrjj

          I'm not sure why... but now it is working. I introduced some modifications from the example you posted. At least I had a mistake in the path of the working directory.

          The last thing related with this issue. When I read the standard output, the process (problem I run in the shell) must finish. The ls command is very fast, but the script I need to run is not ls, and the script can take some hours. I would like to show in real time the standard output to the users, instead of waiting until the end of the process. Is this possible?

          Many thanks for all your help!

          I copy here the final code, perhaps someone else have my problem

          void MainWindow::on_browseParticles_clicked()
          {
          using namespace std;
          cout<<"checking the use of ls in qt * \n";
          QProcess process;
          process.setWorkingDirectory("/home/grullo");

          QString Command;    //Contains the command to be executed
          QStringList args;   //Contains arguments of the command
          
          Command = "ls";
          args<<"-l"<<"/home/vilas";
          
          process.start(Command, args, QIODevice::ReadOnly); //Starts execution of command
          
          process.waitForFinished();
          QString StdOut = process.readAllStandardOutput(); //Reads standard output
          cout<<"\n Printing the standard output..........\n";
          cout<<endl<<StdOut.toStdString();
          
          cout<<"\n Printing the standard error..........\n";
          QString StdError    =   process.readAllStandardError();   //Reads standard error
          cout<<endl<<StdError.toStdString();
          

          }

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #6

          @grullo said in Run command line from Qt app in linux:

          I would like to show in real time the standard output to the users, instead of waiting until the end of the process. Is this possible?

          Yes, absolutely you can, but you must change where you read from the sub-process to use signals & slots. Set up to use readyReadStandardOutput/Error signals, attached to your slots which make your readAllStandardOutput/Error() calls. Remove the process.waitForFinished(), instead put a slot on finished signal. Allow your on_browseParticles_clicked() to exit after the process.start() call, so that it returns to the main Qt event loop. The process now runs in the background, sending you output to capture till finished is received.

          You should also slot onto other signals, like errorOccurred. You may also want to use setProcessChannelMode(QProcess::MergedChannels).

          All the above are in the docs page. Be aware that it is possible that your sub-process's output will not be received by your call program at the moment it is produced. It may be buffered at the sending side, till a newline is output or a certain number of bytes have been produced, and you may not be able to do anything about that (unless you have the source of the sub-process).

          1 Reply Last reply
          3
          • G Offline
            G Offline
            grullo
            wrote on last edited by
            #7

            Thank you very much JoB!

            I will try that later, because I need to have a beta version working by the end of the week.

            Unfortunately, I have other problem and I need your advise again... (sorry I'm new in Qt).

            My previous doubt was how to run a command line in a shell. Now my problem is the "same", but I need to run my own program. What I did: Easy, just substituting the "ls -l" by the command to execute my program. But... but program requires to load some libraries... so what I do is

            //Load the libraries (it is a script)
            QProcess proc;
            QString Command = " . /home/grullo/xmip/xmipp.bashrc;
            proc.start(Command);
            proc.waitForFinished();

            //Now I launch my script
            Command = "myscript";
            proc.start(Command);
            proc.waitForFinished();
            QString StdOut = proc.readAllStandardOutput(); //Reads standard output

            cout<<"\n Printing the standard output..........\n";
            cout<<endl<<StdOut.toStdString();
            cout<<"\n Printing the standard error..........\n";
            QString StdError = proc.readAllStandardError(); //Reads standard error
            cout<<endl<<StdError.toStdString();

            If I lauch my script in command line I get an output in the terminal. But not with Qt program. Also I checked and the script never was executed! Am I doing anything wrong?

            Thanks in advance and sorry I don't want to bother other people with my code

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #8

              Where is your "myscript" located ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • G Offline
                G Offline
                grullo
                wrote on last edited by
                #9

                Thanks SGaist

                myscript is a file that makes use of the libraries loaded with xmip.bashrc

                In fact, fact my post I tried to run myscript providing the absolute path. I mean

                QProcess proc;
                QString Command = " /home/grullo/xmip/build/myscript";
                proc.start(Command);

                Then an error in the StdError appeared " error while loading shared libraries" that suggest me that the loaded libraries (by the first start process) are not available in the second start process. I think that the start processes are independent each other. I cannot load libraries for the second one. Currently the alternative I'm thinking is to create a bash script with all I need to do. Then I will run that script with just one start process.

                I am correct with the idea of independent process?

                Thanks in advacne

                Pablo J. RoginaP 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #10

                  Yes you are because they are completely unrelated.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • G grullo

                    Thanks SGaist

                    myscript is a file that makes use of the libraries loaded with xmip.bashrc

                    In fact, fact my post I tried to run myscript providing the absolute path. I mean

                    QProcess proc;
                    QString Command = " /home/grullo/xmip/build/myscript";
                    proc.start(Command);

                    Then an error in the StdError appeared " error while loading shared libraries" that suggest me that the loaded libraries (by the first start process) are not available in the second start process. I think that the start processes are independent each other. I cannot load libraries for the second one. Currently the alternative I'm thinking is to create a bash script with all I need to do. Then I will run that script with just one start process.

                    I am correct with the idea of independent process?

                    Thanks in advacne

                    Pablo J. RoginaP Offline
                    Pablo J. RoginaP Offline
                    Pablo J. Rogina
                    wrote on last edited by Pablo J. Rogina
                    #11

                    @grullo said in Run command line from Qt app in linux:

                    //Load the libraries (it is a script)

                    More than loading the libraries, it seems to me that you're setting the environment for such libraries to be found/used, setting the path, seeting some environment variables, etc. right?.

                    So I imagine you can run the first command (.bashrc script) and just grab the system environment just after it via QProcessEnvironment::systemEnvironment().

                    Then, use that QProcessEnvironment object that you received and used it to set the environment of the second process via QProcess::setProcessEnvironment

                    Upvote the answer(s) that helped you solve the issue
                    Use "Topic Tools" button to mark your post as Solved
                    Add screenshots via postimage.org
                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                    JonBJ 1 Reply Last reply
                    1
                    • Pablo J. RoginaP Pablo J. Rogina

                      @grullo said in Run command line from Qt app in linux:

                      //Load the libraries (it is a script)

                      More than loading the libraries, it seems to me that you're setting the environment for such libraries to be found/used, setting the path, seeting some environment variables, etc. right?.

                      So I imagine you can run the first command (.bashrc script) and just grab the system environment just after it via QProcessEnvironment::systemEnvironment().

                      Then, use that QProcessEnvironment object that you received and used it to set the environment of the second process via QProcess::setProcessEnvironment

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by JonB
                      #12

                      @Pablo-J-Rogina
                      It might depend exactly what you mean, but I don't think it can work. You cannot run the .bashrc as a QProcess from a Qt program and then use QProcessEnvironment::systemEnvironment() to access the environment which was created in the sub-shell. The systemEnvironment function returns the environment of the calling process (your Qt program), not that of the called bash which interprets the .bashrc file.

                      @grullo
                      Let us assume @Pablo-J-Rogina 's hunch is correct that you need the environment variables created in xmipp.bashrc to be set when running your myscript.

                      Because you presently execute these two files as separate processes, your second command will not inherit anything from executing your first command.

                      What you may be looking for, try

                      proc.start("bash", QStringList() << "-c" << ". /home/grullo/xmip/xmipp.bashrc;  /home/grullo/xmip/build/myscript");
                      

                      or

                      proc.start("bash", QStringList() << "-c" << "source /home/grullo/xmip/xmipp.bashrc;  /home/grullo/xmip/build/myscript");
                      

                      Or, it is possible you could edit /home/grullo/xmip/build/myscript to go source /home/grullo/xmip/xmipp.bashrc as the first thing it does, and then you may able to execute myscript alone from your Qt program.

                      1 Reply Last reply
                      3
                      • G Offline
                        G Offline
                        grullo
                        wrote on last edited by
                        #13

                        @Pablo-J-Rogina

                        Thank's for your reply, what you said is exactly what it is. First I set some enviroments variables, to be used by the script.

                        What I could do is to create the shell script by code , and then call with the start the created script. This worked, but I know that's not a good or a very elegant solution.

                        Then I saw the @JonB solution, and I tested it. It works properly, and its the right solution (not mine dirty script solution) Thanks!. In fact what you propose I tried it without the bash -c, I think that's critical. Good to know it

                        Anyway I will spend some time trying to set the enviroment as @Pablo-J-Rogina suggested, just to learn.

                        Now I will try to read in real time the standardOutput.

                        Thank you to everyone!

                        Pablo J. RoginaP 1 Reply Last reply
                        0
                        • G grullo

                          @Pablo-J-Rogina

                          Thank's for your reply, what you said is exactly what it is. First I set some enviroments variables, to be used by the script.

                          What I could do is to create the shell script by code , and then call with the start the created script. This worked, but I know that's not a good or a very elegant solution.

                          Then I saw the @JonB solution, and I tested it. It works properly, and its the right solution (not mine dirty script solution) Thanks!. In fact what you propose I tried it without the bash -c, I think that's critical. Good to know it

                          Anyway I will spend some time trying to set the enviroment as @Pablo-J-Rogina suggested, just to learn.

                          Now I will try to read in real time the standardOutput.

                          Thank you to everyone!

                          Pablo J. RoginaP Offline
                          Pablo J. RoginaP Offline
                          Pablo J. Rogina
                          wrote on last edited by
                          #14

                          @grullo said in Run command line from Qt app in linux:

                          Anyway I will spend some time trying to set the enviroment as @Pablo-J-Rogina suggested, just to learn.

                          Save time! My approach won't work
                          @JonB was exactly right. QProcess::systemEnvironment() returns the environment of the Qt app, i.e. the calling process and no sub-process can change the parent environment. That's why you need to source the initial script to alter the environment, since source is a shell built-in command that doesn't create another process...

                          Upvote the answer(s) that helped you solve the issue
                          Use "Topic Tools" button to mark your post as Solved
                          Add screenshots via postimage.org
                          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                          1 Reply Last reply
                          2
                          • G Offline
                            G Offline
                            grullo
                            wrote on last edited by
                            #15

                            Thank you anyway @Pablo-J-Rogina.

                            Now I'm able to run scripts, I'm trying to read in real time the standard output. I'm finding some problems, but I will keep fighting them by myself. If for tomorrow I'm not able I will ask you help

                            Thank you everybody!

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              grullo
                              wrote on last edited by grullo
                              #16

                              Dear all,

                              I continue fighting with my app... and I reached a point in which I'm stuck... Of course is again with the issue of launching a shell script. When I click on a button, the app launches an script. However, the script never finish, and always appears this error:

                              standard Error:
                              QProcess: Destroyed while process ("bash") is still running.

                              If I run the script outside of Qt it finishes.

                              I copy here the code I have, just if it helps

                              QProcess proc;
                              proc.setWorkingDirectory("/home/grullo/isoXmipp/");
                              QString cmdline = ". /home/grullo/xmip-bundle/build/xmipp.bashrc";
                              QString xmipCmdLine;
                              createXmippScript(xmipCmdLine);
                              cmdline = cmdline + "; " + xmipCmdLine;
                              std::cout << cmdline.toStdString() << std::endl;
                              proc.start("bash", QStringList() << "-c" <<cmdline);
                              proc.waitForFinished();

                              QString StdOut = proc.readAllStandardOutput(); //Reads standard output
                              ui->consoleOutput->setText(StdOut);
                              cout<<"\n standard output..........\n";
                              cout<<endl<<StdOut.toStdString();

                              cout<<"\n standard error..........\n";
                              QString StdError = proc.readAllStandardError(); //Reads standard error
                              cout<<endl<<StdError.toStdString();

                              Thanks in advance

                              Pablo J. RoginaP 1 Reply Last reply
                              0
                              • G grullo

                                Dear all,

                                I continue fighting with my app... and I reached a point in which I'm stuck... Of course is again with the issue of launching a shell script. When I click on a button, the app launches an script. However, the script never finish, and always appears this error:

                                standard Error:
                                QProcess: Destroyed while process ("bash") is still running.

                                If I run the script outside of Qt it finishes.

                                I copy here the code I have, just if it helps

                                QProcess proc;
                                proc.setWorkingDirectory("/home/grullo/isoXmipp/");
                                QString cmdline = ". /home/grullo/xmip-bundle/build/xmipp.bashrc";
                                QString xmipCmdLine;
                                createXmippScript(xmipCmdLine);
                                cmdline = cmdline + "; " + xmipCmdLine;
                                std::cout << cmdline.toStdString() << std::endl;
                                proc.start("bash", QStringList() << "-c" <<cmdline);
                                proc.waitForFinished();

                                QString StdOut = proc.readAllStandardOutput(); //Reads standard output
                                ui->consoleOutput->setText(StdOut);
                                cout<<"\n standard output..........\n";
                                cout<<endl<<StdOut.toStdString();

                                cout<<"\n standard error..........\n";
                                QString StdError = proc.readAllStandardError(); //Reads standard error
                                cout<<endl<<StdError.toStdString();

                                Thanks in advance

                                Pablo J. RoginaP Offline
                                Pablo J. RoginaP Offline
                                Pablo J. Rogina
                                wrote on last edited by
                                #17

                                @grullo said in Run command line from Qt app in linux:

                                QString cmdline = ". /home/grullo/xmip-bundle/build/xmipp.bashrc";

                                I guess the initial dot (".") is making your command line relative, so that path is not found when set the working directory.

                                In addition, it'd be useful if you handle signal errorOccurred as it helps knowing about any possible error.

                                Upvote the answer(s) that helped you solve the issue
                                Use "Topic Tools" button to mark your post as Solved
                                Add screenshots via postimage.org
                                Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                JonBJ 1 Reply Last reply
                                2
                                • Pablo J. RoginaP Pablo J. Rogina

                                  @grullo said in Run command line from Qt app in linux:

                                  QString cmdline = ". /home/grullo/xmip-bundle/build/xmipp.bashrc";

                                  I guess the initial dot (".") is making your command line relative, so that path is not found when set the working directory.

                                  In addition, it'd be useful if you handle signal errorOccurred as it helps knowing about any possible error.

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on last edited by JonB
                                  #18

                                  @Pablo-J-Rogina
                                  Hi Pablo. It's not what you say. If you read the discussion so far, you will discover that the OP's command-line (passed to bash -c) is deliberately:

                                  . /home/grullo/xmip-bundle/build/xmipp.bashrc
                                  

                                  There is a space after the dot. That is a bash/sh command which is a synonym for source, and reads the following argument as a file of commands into bash to be executed. Personally, while . command may be convenient for typing at the keyboard, I would always use source in a program/script for clarity.

                                  @grullo
                                  First, please do as @Pablo-J-Rogina asks and put a slot on errorOccurred signal. Please also check (and debug out) the return result from proc.waitForFinished();. Do yourself a favour, especially if you having a problem and trying to debug, always check documentation for functions having return results/error indicators and use them.

                                  The implication of the error message you see is that you have allowed the QProcess proc; variable to go out of scope while the process is still running. I don't see how that would arise from the code as shown, but there you are. If it were me, I'd try some other command-lines and see whether you always get this or whether it's only with your particular command.

                                  Ah, hang on. bool QProcess::waitForFinished(int msecs = 30000) times out by default after 30 seconds if not finished. You don't say anything, but I guess you have to wait 30 seconds and then you get the error? (You would have seen this from printing its return result, as I said above, please show us that.) In that case, your code will read any output and then proc will be destroyed. Your sub-process will still be running though, and you will get the message you see. Is that it? Assuming so, you need to decide what you're going to do if the process does not run to conclusion in 30 seconds. That is the most obvious explanation to me of what must be happening? You really ought not be using waitForFinished(), and instead be doing it with signals and slots....

                                  Pablo J. RoginaP 1 Reply Last reply
                                  2
                                  • JonBJ JonB

                                    @Pablo-J-Rogina
                                    Hi Pablo. It's not what you say. If you read the discussion so far, you will discover that the OP's command-line (passed to bash -c) is deliberately:

                                    . /home/grullo/xmip-bundle/build/xmipp.bashrc
                                    

                                    There is a space after the dot. That is a bash/sh command which is a synonym for source, and reads the following argument as a file of commands into bash to be executed. Personally, while . command may be convenient for typing at the keyboard, I would always use source in a program/script for clarity.

                                    @grullo
                                    First, please do as @Pablo-J-Rogina asks and put a slot on errorOccurred signal. Please also check (and debug out) the return result from proc.waitForFinished();. Do yourself a favour, especially if you having a problem and trying to debug, always check documentation for functions having return results/error indicators and use them.

                                    The implication of the error message you see is that you have allowed the QProcess proc; variable to go out of scope while the process is still running. I don't see how that would arise from the code as shown, but there you are. If it were me, I'd try some other command-lines and see whether you always get this or whether it's only with your particular command.

                                    Ah, hang on. bool QProcess::waitForFinished(int msecs = 30000) times out by default after 30 seconds if not finished. You don't say anything, but I guess you have to wait 30 seconds and then you get the error? (You would have seen this from printing its return result, as I said above, please show us that.) In that case, your code will read any output and then proc will be destroyed. Your sub-process will still be running though, and you will get the message you see. Is that it? Assuming so, you need to decide what you're going to do if the process does not run to conclusion in 30 seconds. That is the most obvious explanation to me of what must be happening? You really ought not be using waitForFinished(), and instead be doing it with signals and slots....

                                    Pablo J. RoginaP Offline
                                    Pablo J. RoginaP Offline
                                    Pablo J. Rogina
                                    wrote on last edited by
                                    #19

                                    @JonB said in Run command line from Qt app in linux:

                                    There is a space after the dot. That is a bash/sh command which is a synonym for source,

                                    you're right. That's the problem to post an answer at midnight...

                                    Upvote the answer(s) that helped you solve the issue
                                    Use "Topic Tools" button to mark your post as Solved
                                    Add screenshots via postimage.org
                                    Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                                    JonBJ 1 Reply Last reply
                                    0
                                    • Pablo J. RoginaP Pablo J. Rogina

                                      @JonB said in Run command line from Qt app in linux:

                                      There is a space after the dot. That is a bash/sh command which is a synonym for source,

                                      you're right. That's the problem to post an answer at midnight...

                                      JonBJ Online
                                      JonBJ Online
                                      JonB
                                      wrote on last edited by
                                      #20

                                      @Pablo-J-Rogina
                                      :) It's also why choosing to write source rather than . would make it clearer for the OP and anyone reading the code :)

                                      1 Reply Last reply
                                      1
                                      • G Offline
                                        G Offline
                                        grullo
                                        wrote on last edited by
                                        #21

                                        Thanks for your replies, I'm sorry for the delay in my response. This week we had a peak of work.

                                        Fortunately I could fix it with startDetached. I'm not sure exactly, why doing this the script runs until finishing, but it's working.

                                        Kind regards

                                        1 Reply Last reply
                                        0
                                        • Pl45m4P Pl45m4 referenced this topic on

                                        • Login

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