1

I have a node.js application, which connect everyday to a server.

On this server, a new version of the app can be available, if so, the installed app download it, check if the download is complete, and if so, stop itself calling a shell script, which replace the old app by the new one, and start it.

I m struggling at starting the update script.

I know I can start it with child_process_execFile function, which I do:

var execF = require('child_process').execFile;
var PATH = process.argv[1].substr(0, process.argv[1].lastIndexOf('/')+1),
    filename = 'newapp.js',
execF(PATH + 'up.sh', [PATH + filename], function () {console.log('done'); return ;});

up.sh, for now is just:

cat $1 > /home/pi/test

I get 'done' printed in the console, but test isn t created.

I know that execFile create a subprocess, is it what block the script to do that?

If I suceed to start this, I know I only have to make some cp in the script to have my app auto-updating.

EDIT:

Started as usual (calling the script from console), it work well, is there a reason for the script to don t execute when called from node.js?

4
  • Have you double checked that PATH is the full path and not relative? Commented Oct 31, 2013 at 17:04
  • PATH is '/home/pi/folder/', so a full path yes Commented Oct 31, 2013 at 17:06
  • Have you looked at modules that do this automatically? stackoverflow.com/questions/8918508/… Commented Oct 31, 2013 at 18:19
  • @WiredPrairie: It s almost perfect, could you post this as answer so I can accept it? Also, how do I get rid from the '[always]' before each output? Commented Nov 7, 2013 at 12:32

2 Answers 2

1

I'd suggest that you consider using a module that can do this for you automatically rather than duplicating the effort. Or, at least use their technique as inspiration for you own requirements.

One example is: https://github.com/edwardhotchkiss/always

It's simple to use:

Usage: always <app.js>
=> always app.js

Then, anytime your code changes, the app is killed, and restarted.

As you can see in the source, it uses the Monitor class to watch a specified file, and then uses spawn to kick it off (and of course kill to end the process when a change has happened).

Unfortunately, the [always] output is currently hardcoded into the code, but it would be a simple change/pull request I'm sure to make it optional/configurable. If the author doesn't accept your change, you could just modify a local copy of the code (as it's quite simple overall).

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

Comments

0

Make sure when you spawn/exec the process you are executing the shell that will be processing the script and not the script itself.

Should be something like

execF("/usr/bin/sh", [PATH + 'up.sh', PATH + filename]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.