Killing a FFmpeg process triggered by spawn in NodeJs











up vote
1
down vote

favorite












I am facing an issue wanting to kill a FFmpeg process triggered by spawn from the child_process native NodeJs package.



Here is the script i use to trigger the ffmpeg process.



Assume that the conversion takes a long time, like about 2 hours.



/**
* Execute a command line cmd
* with the arguments in options given as an array of string
* processStore is an array that will store the process during its execution
* and remove it at the end of the command or when error occurs
*/
function execCommandLine({
cmd,
options = ,
processStore = ,
}) {
return new Promise((resolve, reject) => {
const spwaned_process = childProcess.spawn(cmd, options);

// Append the process to a buffer to keep track on it
processStore.push(spwaned_process);

// Do nothing about stdout
spwaned_process.stdout.on('data', () => true);

// Do nothing about stderr
spwaned_process.stderr.on('data', () => true);

spwaned_process.on('close', () => {
const index = processStore.indexOf(spwaned_process);

if (index !== -1) {
processStore.splice(index, 1);
}

resolve();
});

spwaned_process.on('error', () => {
const index = processStore.indexOf(spwaned_process);

if (index !== -1) {
processStore.splice(index, 1);
}

reject();
});
});
}

const processStore = ;

await execCommandLine({
cmd: 'ffmpeg',

options: [
'-i',
'/path/to/input',
'-c:v',
'libvpx-vp9',
'-strict',
'-2',
'-crf',
'30',
'-b:v',
'0',
'-vf',
'scale=1920:1080',
'/path/to/output',
],

processStore,
});


During the conversion, the following code is called to kill all process into the processStore, including the FFmpeg process that is triggered.



// getProcessStore() returns the const processStore array of the above script
getProcessStore().forEach(x => x.kill());

process.exit();


After the program exit, when i run ps -ef | grep ffmpeg, there is still some FFmpeg process running.




root 198 1 0 09:26 ? 00:00:00 ffmpeg -i /path/to/input -ss 00:01:47 -vframes 1 /path/to/output



root 217 1 0 09:26 ? 00:00:00 ps -ef




Do you have an idea about the way to kill a ffmpeg process properly ?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I am facing an issue wanting to kill a FFmpeg process triggered by spawn from the child_process native NodeJs package.



    Here is the script i use to trigger the ffmpeg process.



    Assume that the conversion takes a long time, like about 2 hours.



    /**
    * Execute a command line cmd
    * with the arguments in options given as an array of string
    * processStore is an array that will store the process during its execution
    * and remove it at the end of the command or when error occurs
    */
    function execCommandLine({
    cmd,
    options = ,
    processStore = ,
    }) {
    return new Promise((resolve, reject) => {
    const spwaned_process = childProcess.spawn(cmd, options);

    // Append the process to a buffer to keep track on it
    processStore.push(spwaned_process);

    // Do nothing about stdout
    spwaned_process.stdout.on('data', () => true);

    // Do nothing about stderr
    spwaned_process.stderr.on('data', () => true);

    spwaned_process.on('close', () => {
    const index = processStore.indexOf(spwaned_process);

    if (index !== -1) {
    processStore.splice(index, 1);
    }

    resolve();
    });

    spwaned_process.on('error', () => {
    const index = processStore.indexOf(spwaned_process);

    if (index !== -1) {
    processStore.splice(index, 1);
    }

    reject();
    });
    });
    }

    const processStore = ;

    await execCommandLine({
    cmd: 'ffmpeg',

    options: [
    '-i',
    '/path/to/input',
    '-c:v',
    'libvpx-vp9',
    '-strict',
    '-2',
    '-crf',
    '30',
    '-b:v',
    '0',
    '-vf',
    'scale=1920:1080',
    '/path/to/output',
    ],

    processStore,
    });


    During the conversion, the following code is called to kill all process into the processStore, including the FFmpeg process that is triggered.



    // getProcessStore() returns the const processStore array of the above script
    getProcessStore().forEach(x => x.kill());

    process.exit();


    After the program exit, when i run ps -ef | grep ffmpeg, there is still some FFmpeg process running.




    root 198 1 0 09:26 ? 00:00:00 ffmpeg -i /path/to/input -ss 00:01:47 -vframes 1 /path/to/output



    root 217 1 0 09:26 ? 00:00:00 ps -ef




    Do you have an idea about the way to kill a ffmpeg process properly ?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am facing an issue wanting to kill a FFmpeg process triggered by spawn from the child_process native NodeJs package.



      Here is the script i use to trigger the ffmpeg process.



      Assume that the conversion takes a long time, like about 2 hours.



      /**
      * Execute a command line cmd
      * with the arguments in options given as an array of string
      * processStore is an array that will store the process during its execution
      * and remove it at the end of the command or when error occurs
      */
      function execCommandLine({
      cmd,
      options = ,
      processStore = ,
      }) {
      return new Promise((resolve, reject) => {
      const spwaned_process = childProcess.spawn(cmd, options);

      // Append the process to a buffer to keep track on it
      processStore.push(spwaned_process);

      // Do nothing about stdout
      spwaned_process.stdout.on('data', () => true);

      // Do nothing about stderr
      spwaned_process.stderr.on('data', () => true);

      spwaned_process.on('close', () => {
      const index = processStore.indexOf(spwaned_process);

      if (index !== -1) {
      processStore.splice(index, 1);
      }

      resolve();
      });

      spwaned_process.on('error', () => {
      const index = processStore.indexOf(spwaned_process);

      if (index !== -1) {
      processStore.splice(index, 1);
      }

      reject();
      });
      });
      }

      const processStore = ;

      await execCommandLine({
      cmd: 'ffmpeg',

      options: [
      '-i',
      '/path/to/input',
      '-c:v',
      'libvpx-vp9',
      '-strict',
      '-2',
      '-crf',
      '30',
      '-b:v',
      '0',
      '-vf',
      'scale=1920:1080',
      '/path/to/output',
      ],

      processStore,
      });


      During the conversion, the following code is called to kill all process into the processStore, including the FFmpeg process that is triggered.



      // getProcessStore() returns the const processStore array of the above script
      getProcessStore().forEach(x => x.kill());

      process.exit();


      After the program exit, when i run ps -ef | grep ffmpeg, there is still some FFmpeg process running.




      root 198 1 0 09:26 ? 00:00:00 ffmpeg -i /path/to/input -ss 00:01:47 -vframes 1 /path/to/output



      root 217 1 0 09:26 ? 00:00:00 ps -ef




      Do you have an idea about the way to kill a ffmpeg process properly ?










      share|improve this question













      I am facing an issue wanting to kill a FFmpeg process triggered by spawn from the child_process native NodeJs package.



      Here is the script i use to trigger the ffmpeg process.



      Assume that the conversion takes a long time, like about 2 hours.



      /**
      * Execute a command line cmd
      * with the arguments in options given as an array of string
      * processStore is an array that will store the process during its execution
      * and remove it at the end of the command or when error occurs
      */
      function execCommandLine({
      cmd,
      options = ,
      processStore = ,
      }) {
      return new Promise((resolve, reject) => {
      const spwaned_process = childProcess.spawn(cmd, options);

      // Append the process to a buffer to keep track on it
      processStore.push(spwaned_process);

      // Do nothing about stdout
      spwaned_process.stdout.on('data', () => true);

      // Do nothing about stderr
      spwaned_process.stderr.on('data', () => true);

      spwaned_process.on('close', () => {
      const index = processStore.indexOf(spwaned_process);

      if (index !== -1) {
      processStore.splice(index, 1);
      }

      resolve();
      });

      spwaned_process.on('error', () => {
      const index = processStore.indexOf(spwaned_process);

      if (index !== -1) {
      processStore.splice(index, 1);
      }

      reject();
      });
      });
      }

      const processStore = ;

      await execCommandLine({
      cmd: 'ffmpeg',

      options: [
      '-i',
      '/path/to/input',
      '-c:v',
      'libvpx-vp9',
      '-strict',
      '-2',
      '-crf',
      '30',
      '-b:v',
      '0',
      '-vf',
      'scale=1920:1080',
      '/path/to/output',
      ],

      processStore,
      });


      During the conversion, the following code is called to kill all process into the processStore, including the FFmpeg process that is triggered.



      // getProcessStore() returns the const processStore array of the above script
      getProcessStore().forEach(x => x.kill());

      process.exit();


      After the program exit, when i run ps -ef | grep ffmpeg, there is still some FFmpeg process running.




      root 198 1 0 09:26 ? 00:00:00 ffmpeg -i /path/to/input -ss 00:01:47 -vframes 1 /path/to/output



      root 217 1 0 09:26 ? 00:00:00 ps -ef




      Do you have an idea about the way to kill a ffmpeg process properly ?







      node.js ffmpeg spawn kill-process






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 9:56









      Alrick

      1057




      1057
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          As stated in the node.js documentation of subprocess.kill([signal]), the default sent signal is SIGTERM.



          Ffmpeg do not terminature receiving SIGTERM as explained by @sashoalm in here.




          Newer versions of ffmpeg don't use 'q' anymore, at least on Ubuntu
          Oneiric, instead they say to press Ctrl+C to stop them. So with a
          newer version you can simply use 'killall -INT' to send them SIGINT
          instead of SIGTERM, and they should exit cleanly.






          So call x.kill('SIGINT');






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428263%2fkilling-a-ffmpeg-process-triggered-by-spawn-in-nodejs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            As stated in the node.js documentation of subprocess.kill([signal]), the default sent signal is SIGTERM.



            Ffmpeg do not terminature receiving SIGTERM as explained by @sashoalm in here.




            Newer versions of ffmpeg don't use 'q' anymore, at least on Ubuntu
            Oneiric, instead they say to press Ctrl+C to stop them. So with a
            newer version you can simply use 'killall -INT' to send them SIGINT
            instead of SIGTERM, and they should exit cleanly.






            So call x.kill('SIGINT');






            share|improve this answer

























              up vote
              1
              down vote













              As stated in the node.js documentation of subprocess.kill([signal]), the default sent signal is SIGTERM.



              Ffmpeg do not terminature receiving SIGTERM as explained by @sashoalm in here.




              Newer versions of ffmpeg don't use 'q' anymore, at least on Ubuntu
              Oneiric, instead they say to press Ctrl+C to stop them. So with a
              newer version you can simply use 'killall -INT' to send them SIGINT
              instead of SIGTERM, and they should exit cleanly.






              So call x.kill('SIGINT');






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                As stated in the node.js documentation of subprocess.kill([signal]), the default sent signal is SIGTERM.



                Ffmpeg do not terminature receiving SIGTERM as explained by @sashoalm in here.




                Newer versions of ffmpeg don't use 'q' anymore, at least on Ubuntu
                Oneiric, instead they say to press Ctrl+C to stop them. So with a
                newer version you can simply use 'killall -INT' to send them SIGINT
                instead of SIGTERM, and they should exit cleanly.






                So call x.kill('SIGINT');






                share|improve this answer












                As stated in the node.js documentation of subprocess.kill([signal]), the default sent signal is SIGTERM.



                Ffmpeg do not terminature receiving SIGTERM as explained by @sashoalm in here.




                Newer versions of ffmpeg don't use 'q' anymore, at least on Ubuntu
                Oneiric, instead they say to press Ctrl+C to stop them. So with a
                newer version you can simply use 'killall -INT' to send them SIGINT
                instead of SIGTERM, and they should exit cleanly.






                So call x.kill('SIGINT');







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 at 10:05









                Grégory NEUT

                8,17621437




                8,17621437






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53428263%2fkilling-a-ffmpeg-process-triggered-by-spawn-in-nodejs%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    How to ignore python UserWarning in pytest?

                    Alexandru Averescu