Meteor – Run shell command at server side

1. Install the fibers node package on the server.

npm -g install fibers

 

2. Define the server methods in server.js.
server/server.js

Meteor.startup(function () {
  // Load future from fibers
  var Future = Npm.require("fibers/future");
  // Load exec
  var exec = Npm.require("child_process").exec;

  // Server methods
  Meteor.methods({
    runCode: function () {
      // This method call won't return immediately, it will wait for the
      // asynchronous code to finish, so we call unblock to allow this client
      // to queue other method calls (see Meteor docs)
      this.unblock();
      var future=new Future();
      var command="pwd";
      exec(command,function(error,stdout,stderr){
        if(error){
          console.log(error);
          throw new Meteor.Error(500,command+" failed");
        }
        future.return(stdout.toString());
      });
      return future.wait();
    }
  });
});

 

3. Call the server method on client side.
client/js/main.js

Meteor.call('runCode', function (err, response) {
  console.log(response);
});

 

4. Here is another example if the server method have input parameters.
server/server.js

Meteor.methods({
  runCode: function (x, y) {
  ...

client/js/main.js

Meteor.call('runCode', "value x", "value y", function (err, response) {
  console.log(response);
});

 

Done =)

Reference:

Advertisements

7 thoughts on “Meteor – Run shell command at server side”

  1. npm -g install fibres
    npm ERR! 404 Not Found
    npm ERR! 404
    npm ERR! 404 ‘fibres’ is not in the npm registry.
    npm ERR! 404 You should bug the author to publish it
    npm ERR! 404
    npm ERR! 404 Note that you can also install from a
    npm ERR! 404 tarball, folder, or http url, or git url.

    Is the package by chance “fibers”?

    Like

  2. I have been having some issues…

    I added:

    // load Future
    Future = Npm.require(‘fibers/future’);
    //existing code
    var future=new Future();

    After adding that and running, I get the following in the chromium console:

    Undefined main.js:10

    But in that file, we only go up to line 3.

    Like

  3. runCode: function (x) {
          // This method call won't return immediately, it will wait for the
          // asynchronous code to finish, so we call unblock to allow this client
          // to queue other method calls (see Meteor docs)
          this.unblock();
          console.log(x);
          var ipToPing= x;
          console.log(ipToPing);
          var future=new Future();
          var command="fping 10.58.116.5";
          exec(command,function(error,stdout,stderr){
            if(error){
              console.log(error);
              throw new Meteor.Error(500,command+" failed");
            }
            future.return(stdout.toString());
          });
          return future.wait();
        }
      });
    

    instead of this particular ipaddress in fping “10.58.116.5” , i want to pass the variable ipToPing to it. console.log(ipToPing) is working fine. can we do that? Thanks in advance

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s