简体   繁体   中英

Running tomcat from nodejs

I am trying to use child process in nodejs to launch a tomcat server on local machine. It is am experiment that will help me understand the way child process works and will help with a project I am working on. I've been looking at the documentation here: http://nodejs.org/api/child_process.html but I am having a little of an issue actually doing this.

What I am trying to do is to run nodejs somewhere locally, once I click somewhere (or even launch the page), the page should run a tomcat server, make sure it is up then load the localhost:8080 welcome page. Once I close the page here the nodejs APIs are being called, it should shutdown tomcat (this part is not necessary for now, but just part of the experiment). Thanks!

Take a look of this npm module Shelljs

ShellJS - Unix shell commands for Node.js Build Status

ShellJS is a portable (Windows/Linux/OS X) implementation of Unix shell commands on top of the Node.js API. You can use it to eliminate your shell script's dependency on Unix while still keeping its familiar and powerful commands. You can also install it globally so you can run it from outside Node projects - say goodbye to those gnarly Bash scripts!

ShellJS has been tested in many relevant projects

Since it is able to call an script, then you can control the Tomcat's scripts for starting and shutdown, and keep going with your experiment :)

ShellJS have the option:

exec(command [, options] [, callback])

Available options (all false by default):

 async: Asynchronous execution. Defaults to true if a callback is provided. silent: Do not echo program output to console. 

Examples:

var version = exec('node --version', {silent:true}).output;

var child = exec('some_long_running_process', {async:true}); child.stdout.on('data', function(data) { /* ... do something with data ... */ });

exec('some_long_running_process', function(code, output) {
console.log('Exit code:', code); console.log('Program output:', output); });

Executes the given command synchronously, unless otherwise specified. When in synchronous mode returns the object { code:..., output:... }, containing the program's output (stdout + stderr) and its exit code. Otherwise returns the child process object, and the callback gets the arguments (code, output).

Note: For long-lived processes, it's best to run exec() asynchronously as the current synchronous implementation uses a lot of CPU. This should be getting fixed soon.

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