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. Kill a process and its children

Kill a process and its children

Scheduled Pinned Locked Moved Solved General and Desktop
34 Posts 8 Posters 6.7k 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    I am using a command line program that I started using QProcess and that program uses ffmpeg to complete its task.
    The problem I am having is that I can't kill ffmpeg, I can kill the program that started it but that doesn't kill it.
    I've tried to get the process id and I used it to taskill it but it still doesn't kill ffmpeg.

    In this answer he used wmic on windows, but couldn't figure out how exactly that is done/

    JonBJ D 2 Replies Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you interrupt somehow that program cleanly rather than killing it ?

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

      H 1 Reply Last reply
      1
      • H hbatalha

        I am using a command line program that I started using QProcess and that program uses ffmpeg to complete its task.
        The problem I am having is that I can't kill ffmpeg, I can kill the program that started it but that doesn't kill it.
        I've tried to get the process id and I used it to taskill it but it still doesn't kill ffmpeg.

        In this answer he used wmic on windows, but couldn't figure out how exactly that is done/

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

        @hbatalha
        First it's much preferable if you exit processes cleanly like @SGaist says. Why are you in a position when you need to kill ffmpeg?

        But if you do have to, I suggest you first confirm that you can bring up Task Manager and find and successfully terminate it from there? If you can do that manually we can talk about doing it programatically.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Can you interrupt somehow that program cleanly rather than killing it ?

          H Offline
          H Offline
          hbatalha
          wrote on last edited by
          #4

          @SGaist I looked for a way to interrupt it with Qt but found none

          H 1 Reply Last reply
          0
          • H hbatalha

            @SGaist I looked for a way to interrupt it with Qt but found none

            H Offline
            H Offline
            hbatalha
            wrote on last edited by
            #5

            @JonB said in Kill a process and its children:

            First it's much preferable if you exit processes cleanly like @SGaist says.

            There's no way to exit cleanly with QProcess, I have to kill.

            Why are you in a position when you need to kill ffmpeg?

            Because ffmpeg is a child process of the command line program I am using, when I kill the program with QProcess::kill it will not kill ffmpeg wich is what I wish it could happen.
            For example if I am using that program directly in cmd, when it starts ffmpeg I just need ctrl+c to kill the program along with ffmpeg instance it started. That 's what I am trying figure out how to do with Qt.

            JonBJ SGaistS 2 Replies Last reply
            0
            • H hbatalha

              @JonB said in Kill a process and its children:

              First it's much preferable if you exit processes cleanly like @SGaist says.

              There's no way to exit cleanly with QProcess, I have to kill.

              Why are you in a position when you need to kill ffmpeg?

              Because ffmpeg is a child process of the command line program I am using, when I kill the program with QProcess::kill it will not kill ffmpeg wich is what I wish it could happen.
              For example if I am using that program directly in cmd, when it starts ffmpeg I just need ctrl+c to kill the program along with ffmpeg instance it started. That 's what I am trying figure out how to do with Qt.

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

              @hbatalha said in Kill a process and its children:

              There's no way to exit cleanly with QProcess, I have to kill.

              Nothing to do with QProcess. The question was why ffmpeg has to be killed at all. Why does it not exit normally after doing whatever it is you ask it to do?

              when it starts ffmpeg I just need ctrl+c to kill the program along with ffmpeg instance it started.

              Ctrl+C is a different matter. It is probably sent to ffmpeg when run from command prompt, or something similar. In any case, you are not running it interactively from a command prompt and you say killing the command line parent program does not terminate the ffmpeg, so that is where we are. You can read the discussion in the https://forum.qt.io/topic/86762/can-t-stop-a-process to which you referred.

              I told you that if you can confirm that you are able to run Task Manager, find the ffmpeg process by name and kill it from there, then we could look at doing that from code. But you chose not to answer.

              H 1 Reply Last reply
              0
              • H hbatalha

                I am using a command line program that I started using QProcess and that program uses ffmpeg to complete its task.
                The problem I am having is that I can't kill ffmpeg, I can kill the program that started it but that doesn't kill it.
                I've tried to get the process id and I used it to taskill it but it still doesn't kill ffmpeg.

                In this answer he used wmic on windows, but couldn't figure out how exactly that is done/

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

                @hbatalha Try Detach

                JonBJ 1 Reply Last reply
                0
                • D deleted471

                  @hbatalha Try Detach

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

                  @APM0X400
                  Why do you think that will help? Worth a try I guess, but I would not have thought so.

                  1 Reply Last reply
                  0
                  • H hbatalha

                    @JonB said in Kill a process and its children:

                    First it's much preferable if you exit processes cleanly like @SGaist says.

                    There's no way to exit cleanly with QProcess, I have to kill.

                    Why are you in a position when you need to kill ffmpeg?

                    Because ffmpeg is a child process of the command line program I am using, when I kill the program with QProcess::kill it will not kill ffmpeg wich is what I wish it could happen.
                    For example if I am using that program directly in cmd, when it starts ffmpeg I just need ctrl+c to kill the program along with ffmpeg instance it started. That 's what I am trying figure out how to do with Qt.

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @hbatalha beside the good points of @JonB, what exactly are you using to pilot ffmpeg ?

                    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
                    • JonBJ JonB

                      @hbatalha said in Kill a process and its children:

                      There's no way to exit cleanly with QProcess, I have to kill.

                      Nothing to do with QProcess. The question was why ffmpeg has to be killed at all. Why does it not exit normally after doing whatever it is you ask it to do?

                      when it starts ffmpeg I just need ctrl+c to kill the program along with ffmpeg instance it started.

                      Ctrl+C is a different matter. It is probably sent to ffmpeg when run from command prompt, or something similar. In any case, you are not running it interactively from a command prompt and you say killing the command line parent program does not terminate the ffmpeg, so that is where we are. You can read the discussion in the https://forum.qt.io/topic/86762/can-t-stop-a-process to which you referred.

                      I told you that if you can confirm that you are able to run Task Manager, find the ffmpeg process by name and kill it from there, then we could look at doing that from code. But you chose not to answer.

                      H Offline
                      H Offline
                      hbatalha
                      wrote on last edited by
                      #10

                      @JonB said in Kill a process and its children:

                      Why does it not exit normally after doing whatever it is you ask it to do?

                      It does exit normally after completing its task. But I want to give the user the option to cancel whatever it is doing any time. I can't stop ffmpeg with QProcess because it is a child process of the process QProcess

                      But you chose not to answer.

                      Sorry, I must have missed it. Yes I can go in the task manager and kill with no problem, that's what I have been doing.

                      @SGaist said in Kill a process and its children:

                      what exactly are you using to pilot ffmpeg ?

                      I am using yt-dlp which in turn uses ffmpeg to do some file conversions. Those conversion usually takes a long time, that's why I want to give the user the ability to cancel it anytime.

                      jsulmJ JonBJ 2 Replies Last reply
                      0
                      • H hbatalha

                        @JonB said in Kill a process and its children:

                        Why does it not exit normally after doing whatever it is you ask it to do?

                        It does exit normally after completing its task. But I want to give the user the option to cancel whatever it is doing any time. I can't stop ffmpeg with QProcess because it is a child process of the process QProcess

                        But you chose not to answer.

                        Sorry, I must have missed it. Yes I can go in the task manager and kill with no problem, that's what I have been doing.

                        @SGaist said in Kill a process and its children:

                        what exactly are you using to pilot ffmpeg ?

                        I am using yt-dlp which in turn uses ffmpeg to do some file conversions. Those conversion usually takes a long time, that's why I want to give the user the ability to cancel it anytime.

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @hbatalha What about https://doc.qt.io/qt-5/qprocess.html#kill ?

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

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

                          If the QProcess::kill suggested by @jsulm does not work as expected (which I think should), then try sending the SIGINT signal to the process.

                          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
                          • H hbatalha

                            @JonB said in Kill a process and its children:

                            Why does it not exit normally after doing whatever it is you ask it to do?

                            It does exit normally after completing its task. But I want to give the user the option to cancel whatever it is doing any time. I can't stop ffmpeg with QProcess because it is a child process of the process QProcess

                            But you chose not to answer.

                            Sorry, I must have missed it. Yes I can go in the task manager and kill with no problem, that's what I have been doing.

                            @SGaist said in Kill a process and its children:

                            what exactly are you using to pilot ffmpeg ?

                            I am using yt-dlp which in turn uses ffmpeg to do some file conversions. Those conversion usually takes a long time, that's why I want to give the user the ability to cancel it anytime.

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

                            @hbatalha said in Kill a process and its children:

                            Yes I can go in the task manager and kill with no problem, that's what I have been doing

                            You can try @SGaist's attempt to send the parent process a SIGINT and hope that might interrupt its child, I don't know how that all works on Windows.

                            To get at the ffmpeg process to kill it: you should be able to use EnumProcesses function (psapi.h) to enumerate all running processes and look at their names, and kill the desired one. I came across https://social.msdn.microsoft.com/Forums/en-US/67331e20-a30b-417d-85e5-f9350fef908d/do-we-need-to-use-enumprocesses-and-enumprocessmodules-when-terminating-the-process-in-c?forum=vcgeneral or https://stackoverflow.com/questions/1916574/how-to-effectively-kill-a-process-in-c-win32 you might want to read through. Or Google windows enumprocesses kill process to get going.

                            1 Reply Last reply
                            1
                            • JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on last edited by
                              #14

                              @JonB said in Kill a process and its children:

                              ffmpeg

                              https://stackoverflow.com/questions/9722624/how-to-stop-ffmpeg-remotely

                              1 Reply Last reply
                              1
                              • JoeCFDJ Offline
                                JoeCFDJ Offline
                                JoeCFD
                                wrote on last edited by JoeCFD
                                #15

                                You send out ctrl + C to exit cleanly
                                kill( m_process->processId(), SIGINT );

                                1 Reply Last reply
                                1
                                • H Offline
                                  H Offline
                                  hbatalha
                                  wrote on last edited by hbatalha
                                  #16

                                  I thank everyone's reply here, I was able to find a solution for windows by, as suggested in the replies, sending SIGNT signal to the parent process.

                                  I found the solution in @JonB referenced SO post in this answer and translated it into Qt code:

                                  QProcess::execute("taskkill", {"/pid", QString::number(process->processId()), "/t", "/f"});
                                  

                                  I am yet to test the QProcess::kill behavior in Linux. It would be a bonus if someone who knows telling me.

                                  JoeCFDJ 1 Reply Last reply
                                  0
                                  • H hbatalha

                                    I thank everyone's reply here, I was able to find a solution for windows by, as suggested in the replies, sending SIGNT signal to the parent process.

                                    I found the solution in @JonB referenced SO post in this answer and translated it into Qt code:

                                    QProcess::execute("taskkill", {"/pid", QString::number(process->processId()), "/t", "/f"});
                                    

                                    I am yet to test the QProcess::kill behavior in Linux. It would be a bonus if someone who knows telling me.

                                    JoeCFDJ Offline
                                    JoeCFDJ Offline
                                    JoeCFD
                                    wrote on last edited by JoeCFD
                                    #17

                                    @hbatalha kill( m_process->processId(), SIGINT ); is same as Ctrl + C.
                                    will work on both Windows and Linux. kill() is c code. You do not need taskkill pid on Windows and kill -9 pid on Linux.

                                    H 1 Reply Last reply
                                    0
                                    • JoeCFDJ JoeCFD

                                      @hbatalha kill( m_process->processId(), SIGINT ); is same as Ctrl + C.
                                      will work on both Windows and Linux. kill() is c code. You do not need taskkill pid on Windows and kill -9 pid on Linux.

                                      H Offline
                                      H Offline
                                      hbatalha
                                      wrote on last edited by
                                      #18

                                      @JoeCFD in which header will I find the kill function?

                                      JoeCFDJ 1 Reply Last reply
                                      0
                                      • H hbatalha

                                        @JoeCFD in which header will I find the kill function?

                                        JoeCFDJ Offline
                                        JoeCFDJ Offline
                                        JoeCFD
                                        wrote on last edited by
                                        #19

                                        @hbatalha #include <signal.h>

                                        H 1 Reply Last reply
                                        0
                                        • JoeCFDJ JoeCFD

                                          @hbatalha #include <signal.h>

                                          H Offline
                                          H Offline
                                          hbatalha
                                          wrote on last edited by
                                          #20

                                          @JoeCFD said in Kill a process and its children:

                                          @hbatalha #include <signal.h>

                                          I am still getting undeclared identifier 'kill' error after including <signal.h>

                                          JoeCFDJ 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