MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    child_process won't execute

    Scheduled Pinned Locked Moved Development
    13 Posts 3 Posters 2.3k Views 3 Watching
    Loading More Posts
    • 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.
    • T Offline
      Temisola1
      last edited by

      I’m trying to execute a python script using the child_process. However my function doesn’t seem to be triggering.

      const spawn = require("child_process").spawn
      var NodeHelper = require("node_helper")
      const process = spawn("python3", ["return_something.py"])
      
      module.exports = NodeHelper.create({
        start: function() {
          this.countDown = 10000000
        },
        socketNotificationReceived: function(notification, payload) {
            switch(notification){
                case "DO_YOUR_JOB":
                  console.log(payload)
                  this.sendSocketNotification("I_DID", (this.countDown - payload))
                  break
                case "RETRIEVE_DATA":
                  console.log(payload)
                  this.job()
                  break
            }
        },
        job: function(){
          console.log("I'm trying to retrieve data")
          process.stdout.on('data', (data)=>{
            //********everything works up till here
              console.log("inside")
            var result = String.fromCharCode.apply(null, new Uint16Array((data)))
            this.sendSocketNotification("DATA_RETRIEVED", result)
          })
        }
      })
      

      Python script.

      import sys
      
      print("Hello, I'm Amira")
      
      sys.stdout.flush()
      
      1 Reply Last reply Reply Quote 0
      • S Away
        sdetweil
        last edited by

        how about like this

        const spawn = require("child_process").spawn
        var NodeHelper = require("node_helper")
        
        
        module.exports = NodeHelper.create({
          start: function() {
            this.countDown = 10000000
          },
          socketNotificationReceived: function(notification, payload) {
              switch(notification){
                  case "DO_YOUR_JOB":
                    console.log(payload)
                    this.sendSocketNotification("I_DID", (this.countDown - payload))
                    break
                  case "RETRIEVE_DATA":
                    console.log(payload)
                    this.job()
                    break
              }
          },
          job: function(){
            console.log("I'm trying to retrieve data")
           var process = spawn("python3", ["return_something.py"])
            process.stdout.on('data', (data)=>{
              //********everything works up till here
                console.log("inside")
              var result = String.fromCharCode.apply(null, new Uint16Array((data)))
              this.sendSocketNotification("DATA_RETRIEVED", result)
            })
          }
        })
        

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        T 1 Reply Last reply Reply Quote 0
        • T Offline
          Temisola1 @sdetweil
          last edited by

          @sdetweil thanks for the response. that doesn’t seem to be working either.
          Is there something I need to install to be able to use stdout?

          S 1 Reply Last reply Reply Quote 0
          • S Away
            sdetweil @Temisola1
            last edited by

            @Temisola1 i will look at it in the morning…

            you are seeing the “I’m trying to retrieve data”

            in the terminal window where you did npm start, right?

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            T 1 Reply Last reply Reply Quote 0
            • T Offline
              Temisola1 @sdetweil
              last edited by

              @sdetweil That is correct. It works up till that point.

              S 1 Reply Last reply Reply Quote 0
              • S Away
                sdetweil @Temisola1
                last edited by

                @Temisola1 if you run this does it work from the terminal window?

                testit.py

                import sys 
                # Takes first name and last name via command  
                # line arguments and then display them 
                print("Output from Python") 
                print("First name: " ) 
                print("Last name: " ) 
                

                testit.js

                  var spawn = require("child_process").spawn; 
                
                    // Parameters passed in spawn - 
                    // 1. type_of_script 
                    // 2. list containing Path of the script 
                    //    and arguments for the script  
                
                    // E.g : http://localhost:3000/name?firstname=Mike&lastname=Will 
                    // so, first name = Mike and last name = Will 
                    var process = spawn('python3',["./testit.py"]); 
                   //                         req.query.firstname, 
                     //                       req.query.lastname] ); 
                  
                    // Takes stdout data from script which executed 
                    // with arguments and send this data to res object 
                    process.stdout.on('data', (data)=> { 
                        console.log("received " +data); 
                    } ) 
                

                works for me… (note python script path is ./, make sure u have right path to py file)

                then do
                node testit.js

                Sam

                How to add modules

                learning how to use browser developers window for css changes

                T 2 Replies Last reply Reply Quote 0
                • T Offline
                  Temisola1
                  last edited by

                  @sdetweil said in child_process won't execute:

                  node testit.js

                  Yes, that works in the console.

                  1 Reply Last reply Reply Quote 0
                  • T Offline
                    Temisola1 @sdetweil
                    last edited by

                    @sdetweil So I tried putting in the full path in my node_helper as opposed to relative path and that seemed to work. is there a way I can log the current directory in nodejs. It seems that’s the issue

                    S 1 Reply Last reply Reply Quote 0
                    • T Offline
                      Temisola1 @sdetweil
                      last edited by Temisola1

                      @sdetweil after running successfully a few times it now returns Buffer 48 45 5c… or something that looks like hexadecimal code

                      S 1 Reply Last reply Reply Quote 0
                      • S Away
                        sdetweil @Temisola1
                        last edited by

                        @Temisola1 the node_helper doesn’t know where it is… but the Modulename does…

                        this.path

                        so, you can add that to the config info you send down in the typical

                        sendSocketNotification(“somevalue”, this.config)

                        to pass parameters to the node_helper

                        Sam

                        How to add modules

                        learning how to use browser developers window for css changes

                        1 Reply Last reply Reply Quote 0
                        • 1
                        • 2
                        • 1 / 2
                        • First post
                          Last post
                        Enjoying MagicMirror? Please consider a donation!
                        MagicMirror created by Michael Teeuw.
                        Forum managed by Sam, technical setup by Karsten.
                        This forum is using NodeBB as its core | Contributors
                        Contact | Privacy Policy