0

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?

2
  • My guess is the grade.sh never finishes running so that you do not see any logs in console Commented Dec 8, 2016 at 2:34
  • @Khang I've let it sit for a while and to test if that was the case I added a line at the beginning of the script that would append a line with current time to a file and it never writes. Which implies the file never even starts to execute. Commented Dec 8, 2016 at 6:04

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.