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. QProcess and Ctrl-Z + BG on Linux
QtWS25 Last Chance

QProcess and Ctrl-Z + BG on Linux

Scheduled Pinned Locked Moved Unsolved General and Desktop
qprocess
17 Posts 5 Posters 5.2k Views
  • 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.
  • JonBJ JonB

    @jsulm
    Sorry, but you are incorrect :) [See my EDIT at end, where I correct myself and apologise, I had not seen that the OP had used bg after Ctrl+Z.] Been using Ctrl+Z from terminal processes since (probably) before you were born ;-)

    ctrl+z stops the process and returns you to the current shell. You can now type fg to continue process, or type bg to continue the process in the background.

    See here:

    jon@ubuntu-22:~$ sleep 500
    ^Z
    [1]+  Stopped                 sleep 500
    jon@ubuntu-22:~$ ps -lu jon  | grep sleep
    0 T  1000    2967    2424  0  80   0 -  4256 do_sig pts/0    00:00:00 sleep
    jon@ubuntu-22:~$ bg
    [1]+ sleep 500 &
    jon@ubuntu-22:~$ ps -lu jon  | grep sleep
    0 S  1000    2967    2424  0  80   0 -  4256 do_sys pts/0    00:00:00 sleep
    jon@ubuntu-22:~$ 
    

    Note the following:

    • After pressing Ctrl+Z the shell responds with Stopped. Stopped means ... stopped! Not running.
    • The ps shows T for the state of the sleep. That is for "stopped".
    • It is only when I type bg ("background") that the process is put into the background and at that point allowed to continue. It is only now that it will run but pause if it tries to do input or output. Note the ps now shows the process on S, which is for "sleeping", i.e. running but happening to be sleeping at present.

    The OP mentioned nothing about asking the shell to put it into the background after pressing Ctrl+Z.

    Try the following:

    • From a terminal run gedit. At this point you can type characters into it.
    • Now press Ctrl+Z. Shell reports gedit is "Stopped". Now try typing into its window: nothing happens, the characters are not even echoed. The gedit is stopped and does not accept any input! (After a while Ubuntu/desktop manager reports "program is not responding".)
    • Now type bg. You can interact with the gedit again :)

    EDIT
    Ohhh, I never saw the OP typed bg! That's the trouble with people not using Code to mark this. Now I see it in the title too. Sorry!

    OK, my apologies, we are talking about after typing bg here, right? That changes things!

    @Sameer
    I might investigate this behaviour. As you say, in principle Ctrl+Z followed by bg ought be the same as running it with & from the start. However, one difference is that it receives two Linux signals in the former case, might depend how they were handled.

    Could you make two things clear:

    • Your code includes workerProcess. Is this just the name for your QProcess, what I want to know is whether your code here involves any threads? I will assume/hope not.
    • You press the button which runs QProcess::start() after you have done the suspend/background, right?

    Also, for the record, what is the process you are spawning from QProcess? Is is a non-UI program which (might do) stdin/stdout (like gcc), or is it a process that will use the desktop windowing system (like gedit)?

    kkoehneK Offline
    kkoehneK Offline
    kkoehne
    Moderators
    wrote on last edited by
    #5

    @JonB said in QProcess and Ctrl-Z + BG on Linux:

    The OP mentioned nothing about asking the shell to put it into the background after pressing Ctrl+Z.

    well, the original post reads

    However, if I run it normally, then suspend it using Ctrl-Z and put it in the background, things stop working
    % myprogram
    Ctrl+Z
    bg

    To the original question: I understand you send the GUI process in the background, which launches another process via QProcess. Is the sub-process running already when you send the GUI process to the background? If yes, what's the state of it after you sent the GUI process in the background? Is it actually exiting then later on, and just the finished() signal is missing?

    A minimal reproducible example might help. I'm not ruling out a Qt defect, as the QProcess logic is sometimes a bit special, but it doesn't seem like a known issue at least...

    Director R&D, The Qt Company

    1 Reply Last reply
    0
    • S Sameer

      I have a Qt gui application that runs on Linux.

      It is a QApplication that uses QProcess to do some work when a button is clicked. The finished signal on the QProcess is connected to a slot.

      connect(workerProcess, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(onWorkerFinished(int,QProcess::ExitStatus)));

      Things work fine when I run the program normally on commandline
      % myProgram

      or run it in the background
      % myProgram&

      However, if I run it normally, then suspend it using Ctrl-Z and put it in the background, things stop working
      % myprogram
      Ctrl+Z
      bg

      The GUI still responds. But when I click the button, it seems to start the QProcess but the slot connected to the "finished" signal never gets called.

      Not sure what's going on. Does suspending the application and putting it in the background somehow mess the signal/slot connection?

      Would appreciate any help to fix this?

      Thanks

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

      @Sameer , @jsulm , @kkoehne
      To make up for my earlier misunderstanding, have tried following standalone code:

      #include <QApplication>
      #include <QDebug>
      #include <QLayout>
      #include <QObject>
      #include <QProcess>
      #include <QPushButton>
      #include <QWidget>
      
      class MyWidget : public QWidget
      {
      private:
          QProcess *proc;
      
      public slots:
          void onClicked()
          {
              proc = new QProcess;
              QObject::connect(proc, &QProcess::started, this, []() { qDebug() << "Process started"; } );
              QObject::connect(proc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, []() { qDebug() << "Process finished"; } );
              proc->start("sleep", { "10"} );
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MyWidget w;
          w.setLayout(new QVBoxLayout);
          QPushButton *pb = new QPushButton("QProcess");
          w.layout()->addWidget(pb);
          w.show();
      
          QObject::connect(pb, &QPushButton::clicked, &w, &MyWidget::onClicked);
      
          return a.exec();
      }
      

      Qt 5.15 (I don't have Qt6), Ubuntu 22.04. Run from terminal. Press Ctrl+Z there after launching, then bg. Then press the button. I do see in the terminal from the debug output Process started followed by Process finished 10 seconds later. So it works normally.

      Suggest you first try this, does it work for you? If it does compare against your code to find a difference.

      S 1 Reply Last reply
      2
      • JonBJ JonB

        @jsulm
        Sorry, but you are incorrect :) [See my EDIT at end, where I correct myself and apologise, I had not seen that the OP had used bg after Ctrl+Z.] Been using Ctrl+Z from terminal processes since (probably) before you were born ;-)

        ctrl+z stops the process and returns you to the current shell. You can now type fg to continue process, or type bg to continue the process in the background.

        See here:

        jon@ubuntu-22:~$ sleep 500
        ^Z
        [1]+  Stopped                 sleep 500
        jon@ubuntu-22:~$ ps -lu jon  | grep sleep
        0 T  1000    2967    2424  0  80   0 -  4256 do_sig pts/0    00:00:00 sleep
        jon@ubuntu-22:~$ bg
        [1]+ sleep 500 &
        jon@ubuntu-22:~$ ps -lu jon  | grep sleep
        0 S  1000    2967    2424  0  80   0 -  4256 do_sys pts/0    00:00:00 sleep
        jon@ubuntu-22:~$ 
        

        Note the following:

        • After pressing Ctrl+Z the shell responds with Stopped. Stopped means ... stopped! Not running.
        • The ps shows T for the state of the sleep. That is for "stopped".
        • It is only when I type bg ("background") that the process is put into the background and at that point allowed to continue. It is only now that it will run but pause if it tries to do input or output. Note the ps now shows the process on S, which is for "sleeping", i.e. running but happening to be sleeping at present.

        The OP mentioned nothing about asking the shell to put it into the background after pressing Ctrl+Z.

        Try the following:

        • From a terminal run gedit. At this point you can type characters into it.
        • Now press Ctrl+Z. Shell reports gedit is "Stopped". Now try typing into its window: nothing happens, the characters are not even echoed. The gedit is stopped and does not accept any input! (After a while Ubuntu/desktop manager reports "program is not responding".)
        • Now type bg. You can interact with the gedit again :)

        EDIT
        Ohhh, I never saw the OP typed bg! That's the trouble with people not using Code to mark this. Now I see it in the title too. Sorry!

        OK, my apologies, we are talking about after typing bg here, right? That changes things!

        @Sameer
        I might investigate this behaviour. As you say, in principle Ctrl+Z followed by bg ought be the same as running it with & from the start. However, one difference is that it receives two Linux signals in the former case, might depend how they were handled.

        Could you make two things clear:

        • Your code includes workerProcess. Is this just the name for your QProcess, what I want to know is whether your code here involves any threads? I will assume/hope not.
        • You press the button which runs QProcess::start() after you have done the suspend/background, right?

        Also, for the record, what is the process you are spawning from QProcess? Is is a non-UI program which (might do) stdin/stdout (like gcc), or is it a process that will use the desktop windowing system (like gedit)?

        S Offline
        S Offline
        SameerK
        wrote on last edited by
        #7

        @JonB OP here. My apologies. I didn't have notifications on. So didn't see these useful responses till today.

        I am still trying to solve this problem.
        To answer your questions

        • workerProcess is just the name of the QProcess. No threads involved?
        • I DO press the button which starts the workerProcess AFTER the suspend/background
        • The worker process is doing non UI stuff.

        Hope this answers your questions. Desperately seeking help on this.

        1 Reply Last reply
        0
        • JonBJ JonB

          @Sameer , @jsulm , @kkoehne
          To make up for my earlier misunderstanding, have tried following standalone code:

          #include <QApplication>
          #include <QDebug>
          #include <QLayout>
          #include <QObject>
          #include <QProcess>
          #include <QPushButton>
          #include <QWidget>
          
          class MyWidget : public QWidget
          {
          private:
              QProcess *proc;
          
          public slots:
              void onClicked()
              {
                  proc = new QProcess;
                  QObject::connect(proc, &QProcess::started, this, []() { qDebug() << "Process started"; } );
                  QObject::connect(proc, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, []() { qDebug() << "Process finished"; } );
                  proc->start("sleep", { "10"} );
              }
          };
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MyWidget w;
              w.setLayout(new QVBoxLayout);
              QPushButton *pb = new QPushButton("QProcess");
              w.layout()->addWidget(pb);
              w.show();
          
              QObject::connect(pb, &QPushButton::clicked, &w, &MyWidget::onClicked);
          
              return a.exec();
          }
          

          Qt 5.15 (I don't have Qt6), Ubuntu 22.04. Run from terminal. Press Ctrl+Z there after launching, then bg. Then press the button. I do see in the terminal from the debug output Process started followed by Process finished 10 seconds later. So it works normally.

          Suggest you first try this, does it work for you? If it does compare against your code to find a difference.

          S Offline
          S Offline
          SameerK
          wrote on last edited by SameerK
          #8

          @JonB
          I tried your simple example and it works as expected. I see both the started and finished slots being called even after suspending the application with ctrl+Z and putting it in background.

          However same thing doesn't seem to happen in my code. Unfortunately my code is too complex to put here. But I tried the following experiment.

          Where I start my process, I also started a test process similar to your example.

          m_testProcess = new QProcess;
          QObject::connect(m_testProcess, &QProcess::started, this, []() { qDebug() << "m_testProcess started"; } );
          QObject::connect(m_testProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, []() { qDebug() << "m_testProcess finished"; } );
          m_testProcess->start("sleep", { "1"} );
          

          Before Ctrl-Z + bg, I see both started and finished messages.
          After Ctrl-Z + bg, I see the started message but not the finished message.

          Any idea what might be going on?

          JonBJ 1 Reply Last reply
          0
          • S SameerK

            @JonB
            I tried your simple example and it works as expected. I see both the started and finished slots being called even after suspending the application with ctrl+Z and putting it in background.

            However same thing doesn't seem to happen in my code. Unfortunately my code is too complex to put here. But I tried the following experiment.

            Where I start my process, I also started a test process similar to your example.

            m_testProcess = new QProcess;
            QObject::connect(m_testProcess, &QProcess::started, this, []() { qDebug() << "m_testProcess started"; } );
            QObject::connect(m_testProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, []() { qDebug() << "m_testProcess finished"; } );
            m_testProcess->start("sleep", { "1"} );
            

            Before Ctrl-Z + bg, I see both started and finished messages.
            After Ctrl-Z + bg, I see the started message but not the finished message.

            Any idea what might be going on?

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

            @SameerK said in QProcess and Ctrl-Z + BG on Linux:

            However same thing doesn't seem to happen in my code. Unfortunately my code is too complex to put here.

            I think you know the likely response here! If code works in simple case but not in your "complex" case then probably nobody can guess. You really need to comment out swathes of code till you can find where the different behaviour seems to emanate from.

            A couple of tests here. I don't know what we will make of the answers, but let's gather the information:

            • Where you do your test you say you do this sleep() as well as your process. Eliminate your process for a while, just in case that somehow interferes.
            • Instead of going bg after the Ctrl+Z try fg instead. Any difference?
            • When you do not get the QProcess::finished signal, use ps to find the current state of the sub-process. Is it running? Suspended? Zombied? Removed from running processes and no longer found?
            • Attach slot to signal void QProcess::stateChanged(QProcess::ProcessState newState). What state changes do you get?
            • When setting off sub-process start a QTimer for, say, once per second. Have it report QProcess::ProcessState QProcess::state() const. What state does that repeatedly report?
            S 2 Replies Last reply
            1
            • JonBJ JonB

              @SameerK said in QProcess and Ctrl-Z + BG on Linux:

              However same thing doesn't seem to happen in my code. Unfortunately my code is too complex to put here.

              I think you know the likely response here! If code works in simple case but not in your "complex" case then probably nobody can guess. You really need to comment out swathes of code till you can find where the different behaviour seems to emanate from.

              A couple of tests here. I don't know what we will make of the answers, but let's gather the information:

              • Where you do your test you say you do this sleep() as well as your process. Eliminate your process for a while, just in case that somehow interferes.
              • Instead of going bg after the Ctrl+Z try fg instead. Any difference?
              • When you do not get the QProcess::finished signal, use ps to find the current state of the sub-process. Is it running? Suspended? Zombied? Removed from running processes and no longer found?
              • Attach slot to signal void QProcess::stateChanged(QProcess::ProcessState newState). What state changes do you get?
              • When setting off sub-process start a QTimer for, say, once per second. Have it report QProcess::ProcessState QProcess::state() const. What state does that repeatedly report?
              S Offline
              S Offline
              SameerK
              wrote on last edited by
              #10

              @JonB Will try out your sugeestions.

              Just wanna say that I really appreciate you responding to this "help me find a needle in the haystack" kind of question :)

              1 Reply Last reply
              0
              • JonBJ JonB

                @SameerK said in QProcess and Ctrl-Z + BG on Linux:

                However same thing doesn't seem to happen in my code. Unfortunately my code is too complex to put here.

                I think you know the likely response here! If code works in simple case but not in your "complex" case then probably nobody can guess. You really need to comment out swathes of code till you can find where the different behaviour seems to emanate from.

                A couple of tests here. I don't know what we will make of the answers, but let's gather the information:

                • Where you do your test you say you do this sleep() as well as your process. Eliminate your process for a while, just in case that somehow interferes.
                • Instead of going bg after the Ctrl+Z try fg instead. Any difference?
                • When you do not get the QProcess::finished signal, use ps to find the current state of the sub-process. Is it running? Suspended? Zombied? Removed from running processes and no longer found?
                • Attach slot to signal void QProcess::stateChanged(QProcess::ProcessState newState). What state changes do you get?
                • When setting off sub-process start a QTimer for, say, once per second. Have it report QProcess::ProcessState QProcess::state() const. What state does that repeatedly report?
                S Offline
                S Offline
                SameerK
                wrote on last edited by
                #11

                @JonB said in QProcess and Ctrl-Z + BG on Linux:

                These were very useful suggestions.

                • Where you do your test you say you do this sleep() as well as your process. Eliminate your process for a while, just in case that somehow interferes.
                  Did this. Still the same result.
                • Instead of going bg after the Ctrl+Z try fg instead. Any difference?
                  Doing fg instead bg produces same result.
                • When you do not get the QProcess::finished signal, use ps to find the current state of the sub-process. Is it running? Suspended? Zombied? Removed from running processes and no longer found?
                  Shows up as follows withps - [sleep] <defunct>
                • Attach slot to signal void QProcess::stateChanged(QProcess::ProcessState newState). What state changes do you get?
                  I get Starting and Running, nothing afterwards.
                  Debug: m_runProcess stateChanged - QProcess::Starting
                  Debug: m_runProcess stateChanged - QProcess::Running
                  Debug: m_runProcess started
                • When setting off sub-process start a QTimer for, say, once per second. Have it report QProcess::ProcessState QProcess::state() const. What state does that repeatedly report?
                  Tried this. It reports the process as "Running"
                JonBJ 1 Reply Last reply
                1
                • S SameerK

                  @JonB said in QProcess and Ctrl-Z + BG on Linux:

                  These were very useful suggestions.

                  • Where you do your test you say you do this sleep() as well as your process. Eliminate your process for a while, just in case that somehow interferes.
                    Did this. Still the same result.
                  • Instead of going bg after the Ctrl+Z try fg instead. Any difference?
                    Doing fg instead bg produces same result.
                  • When you do not get the QProcess::finished signal, use ps to find the current state of the sub-process. Is it running? Suspended? Zombied? Removed from running processes and no longer found?
                    Shows up as follows withps - [sleep] <defunct>
                  • Attach slot to signal void QProcess::stateChanged(QProcess::ProcessState newState). What state changes do you get?
                    I get Starting and Running, nothing afterwards.
                    Debug: m_runProcess stateChanged - QProcess::Starting
                    Debug: m_runProcess stateChanged - QProcess::Running
                    Debug: m_runProcess started
                  • When setting off sub-process start a QTimer for, say, once per second. Have it report QProcess::ProcessState QProcess::state() const. What state does that repeatedly report?
                    Tried this. It reports the process as "Running"
                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #12

                  @SameerK said in QProcess and Ctrl-Z + BG on Linux:

                  Shows up as follows withps - [sleep] <defunct>

                  Not sure. From what you say I think the sleep sub-process has exited but not been waited on by its parent. Qt parent still thinks process is running, hence no finished.

                  Really I'm afraid as you know we have demonstrated in a small test program that it behaves OK there. I think you will have to find out what differs in your real program.

                  S 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @SameerK said in QProcess and Ctrl-Z + BG on Linux:

                    Shows up as follows withps - [sleep] <defunct>

                    Not sure. From what you say I think the sleep sub-process has exited but not been waited on by its parent. Qt parent still thinks process is running, hence no finished.

                    Really I'm afraid as you know we have demonstrated in a small test program that it behaves OK there. I think you will have to find out what differs in your real program.

                    S Offline
                    S Offline
                    SameerK
                    wrote on last edited by SameerK
                    #13

                    @JonB

                    Yes that's what seems to be happening.

                    Another thing I discovered. This happens only with an explicit Ctrl+Z.
                    If I send a SIGSTOP to the main application process from another terminal and then put it into background, things are fine.

                    JonBJ 1 Reply Last reply
                    0
                    • S SameerK

                      @JonB

                      Yes that's what seems to be happening.

                      Another thing I discovered. This happens only with an explicit Ctrl+Z.
                      If I send a SIGSTOP to the main application process from another terminal and then put it into background, things are fine.

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

                      @SameerK
                      I had meant to mention testing that. That's ridiculous. But again I'm afraid I don't know what it tells us, nor what you can do about it. I reiterate that since it does not happen on my small test program you are going to have to find out what is different in your complex situation, somehow.

                      S 2 Replies Last reply
                      0
                      • JonBJ JonB

                        @SameerK
                        I had meant to mention testing that. That's ridiculous. But again I'm afraid I don't know what it tells us, nor what you can do about it. I reiterate that since it does not happen on my small test program you are going to have to find out what is different in your complex situation, somehow.

                        S Offline
                        S Offline
                        SameerK
                        wrote on last edited by
                        #15

                        @JonB Doing that now.

                        But once again wanna mention that I truly appreciate you spending time helping me debug this.

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @SameerK
                          I had meant to mention testing that. That's ridiculous. But again I'm afraid I don't know what it tells us, nor what you can do about it. I reiterate that since it does not happen on my small test program you are going to have to find out what is different in your complex situation, somehow.

                          S Offline
                          S Offline
                          SameerK
                          wrote on last edited by
                          #16

                          @JonB I went back to your small test program and ran another experiment and now I can get that to fail too!!!

                          Invoke test application
                          Ctrl-Z
                          bg
                          Press button
                          Things work fine (Get both started and finished signals)

                          Invoke test application
                          Press button
                          Things work fine (Get both started and finished signals)
                          Ctrl-Z
                          bg
                          Press button
                          Things don't work anymore (Get started signal but no finished signal!!!)

                          Btw, I am using Qt5.12 and on RedHat Enterprise Linux 8.6

                          JonBJ 1 Reply Last reply
                          0
                          • S SameerK

                            @JonB I went back to your small test program and ran another experiment and now I can get that to fail too!!!

                            Invoke test application
                            Ctrl-Z
                            bg
                            Press button
                            Things work fine (Get both started and finished signals)

                            Invoke test application
                            Press button
                            Things work fine (Get both started and finished signals)
                            Ctrl-Z
                            bg
                            Press button
                            Things don't work anymore (Get started signal but no finished signal!!!)

                            Btw, I am using Qt5.12 and on RedHat Enterprise Linux 8.6

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

                            @SameerK
                            If it's repeatable you can raise a bug report for it. They will want as small a piece of code for the UI as possible. Pick something standard under Linux for the command (sleep might be suitable).

                            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