[node.js] executing a bash script from node.js

1,984 views
Skip to first unread message

Jose G. Quenum

unread,
Oct 9, 2011, 5:27:38 PM10/9/11
to nod...@googlegroups.com
Hi all,
I am trying to execute bash scripts from within node.js But I am getting a Broken pipe error.

Suppose I have the following bash script
#! /usr/bin/env bash
echo $1
dir_var="/Users/username/Desktop"
new_dir="node_shell"
echo "Changing to Desktop directory"
cd $dir_var

echo "Creating a new folder"
mkdir -p $new_dir

and I execute the script with the following method

create_fs_account_1: (user_name, callback) ->
console.log "Calling script execution"
sh = spawn('sh', [user_name])
fs.createReadStream('public/shell_scripts/shell_test.sh').pipe(sh.stdin)
console.log("Just read the script file")
sh.stdout.on 'data', (data) ->
console.log "New data " + data
sh.on 'exit', (code) ->
console.log "script ended with code " + code
callback code

When I execute the method I get the following error

node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: EPIPE, Broken pipe
at Socket._writeImpl (net.js:159:14)
at Socket._writeOut (net.js:444:25)
at Socket.write (net.js:377:17)
at [object Object].ondata (stream.js:36:26)
at [object Object].emit (events.js:64:17)
at [object Object]._emitData (fs.js:916:10)
at afterRead (fs.js:898:10)
at wrapper (fs.js:245:17)

I have also noticed that when there is no error, the script doesn't really execute. the side effect I am expecting is not there. Can somebody explain to me what I am doing wrong between the script and the node.js (coffee script) code?
thanks in advance

Diogo Resende

unread,
Oct 9, 2011, 5:35:18 PM10/9/11
to nod...@googlegroups.com
1. You should avoid coffee script, at least on this ml (my opinion).
For me is more difficult to debug because I don't know the syntax.

2. Why don't you spawn the script?
> span('sh', [ 'public/....', user_name ])

3. Why do you cd to $dir_var if you mkdir -p?
> mkdir -p $dir_var/$new_dir

Hope it helps :)

---
Diogo R.

Isaac Schlueter

unread,
Oct 9, 2011, 5:43:34 PM10/9/11
to nod...@googlegroups.com
If you spawn sh, it wont' be a login shell, so this isn't going to
work as you expect.

It'd be better to do something like this:

spawn("bash", ["public/shell_scripts/shell_test.sh"])

or even:

spawn("bash", ["public/shell_scripts/shell_test.sh"],
{ cwd: "/Users/username/Desktop", uid: <username uid> })


(And yes, you'll get better responses if you post examples in native
js rather than a to-js language.)

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>

Jose G. Quenum

unread,
Oct 9, 2011, 5:50:42 PM10/9/11
to nod...@googlegroups.com
thanks to y'all for your comments. The script I use in this example is just a dummy stuff to test how to execute bash scripts in node.js. The actual scripts will be ssh commands.

Anyway, let me compile my code in javascript and get back to the group

Isaac Schlueter

unread,
Oct 9, 2011, 5:53:07 PM10/9/11
to nod...@googlegroups.com
On Sun, Oct 9, 2011 at 14:50, Jose G. Quenum <ghis...@gmail.com> wrote:
> thanks to y'all for your comments. The script I use in this example is just a dummy stuff to test how to execute bash scripts in node.js. The actual scripts will be ssh commands.

Note that ssh takes an argument which is the command to run on the
remote server, so you could do this pretty easily in the same way.

spawn("ssh", ["user@remotehost", "chdir blah && ls"])

Jose G. Quenum

unread,
Oct 9, 2011, 5:56:31 PM10/9/11
to nod...@googlegroups.com
changing from sh to bash solves the broken pipe problem. But, still the script as it is doesn't execute. I don't see the folder being created in Desktop. Is there something wrong in the way I call the script?

Ryan Schmidt

unread,
Oct 9, 2011, 11:00:53 PM10/9/11
to nod...@googlegroups.com
On Oct 9, 2011, at 16:50, Jose G. Quenum wrote:

> The actual scripts will be ssh commands.

In that case, you might be interested in existing modules for dealing with ssh, like:

https://github.com/FlashFan/NodeSSH


Jose G. Quenum

unread,
Oct 10, 2011, 7:01:55 AM10/10/11
to nod...@googlegroups.com
cool thanks @ryan. I'll take a look at it as soon as I am done with my tests.
Right now I am still struggling with getting node to execute a simple script.

Floby

unread,
Oct 11, 2011, 2:23:07 AM10/11/11
to nodejs
what is this supposed to do?
fs.createReadStream('public/shell_scripts/
shell_test.sh').pipe(sh.stdin)

Damianos Mylonakis

unread,
Oct 11, 2011, 6:28:46 AM10/11/11
to nod...@googlegroups.com
Create a Readable Stream (async, non-blocking) and when data chunks are available, pipe them to stdin of the spawned process.
pipe is just a proxy. When the data event of the readable stream is fired, it will do sh.write(data).
Reply all
Reply to author
Forward
0 new messages