简体   繁体   中英

Problems running a bash script server side from Meteor

I have a bash script that I am running server side from meteor. I have tested that I am successfully able to run shell commands by running 'ls' and getting the expected response back. However, when I run my shell script no output is ever logged to the server console and none of the intended effects of the script are successful. I am printing stderr,stdout, and error yet they print nothing when I run my script.

 Meteor.methods({
        grade: function(unittest, code) {
            this.unblock();

            var sys = Npm.require('sys');
            var exec = Npm.require('child_process').exec;

            // exec('/bin/ls /srv/srcalyzer/scripts', function(error,stdout,stderr){
            //     console.log('error: ',error);
            //     console.log('stdout: ',stdout);
            //     console.log('stderr: ',stderr);
            // });

            console.log('running grade')
            exec('/bin/bash /srv/srcalyzer/scripts/grade.sh', function(error,stdout,stderr){
                console.log('error: ',error);
                console.log('stdout: ',stdout);
                console.log('stderr: ',stderr);
            });
            console.log('just finished.');    
        },

Although it is currently commented out the /bin/ls /some/path logs the expected output to console. However when I run the /bin/bash /path/to/.sh that I know is in place. The console output looks like

I20161207-15:22:07.031(-5)? running grade
I20161207-15:22:07.045(-5)? just finished.

The script does take a short time to run (~15-20 seconds). I am uncertain if this is relevant or not.

Can someone please help me understand what is happening?

there's a hint here:

I20161207-15:22:07.031(-5)? running grade
I20161207-15:22:07.045(-5)? just finished.

that's only taking a few ms to run. meaning, your Meteor method is likely exiting before the exec() finishes. i've never run an exec() in Meteor, so i'm unsure if that shell script keeps running after your method exits.

what you need is to run a future in your Meteor method, so it doesn't exit until your shell script comes back.

something like:

let future = new Future();

exec('/bin/bash /srv/srcalyzer/scripts/grade.sh', function(error,stdout,stderr){
                console.log('error: ',error);
                console.log('stdout: ',stdout);
                console.log('stderr: ',stderr);

                future.return(stdout.toString());
            });

return future.wait();

now your Meteor method will wait until your script finishes.

(caveat: i didn't try this, just typing this solution from memory on my non-work machine)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM