質問

I'm trying to get create a local API service for testing purposes, which involves running a make command in my build system. The code looks like this:

(note: this is in coffeescript)

request = require "request"
child_process = require "child_process"
tsc = require "training_service_connector"

campaign = "happytest"
strategy = campaign
port_number = 54340
service_conf_filename = tsc.writeServiceConfig(strategy, port_number)
exec_callback = (error, stdout, stderr) -> 
    console.log ('stdout:\n' + stdout + '\nstderr:\n' + stderr + "\nerror:\n" + error)
child_process.exec ("CONFIG=#{service_conf_filename} make run_bidder_service_runner", exec_callback)

# some other stuff

Now, in trying to figure out how to get the rest of the test to run AFTER the API goes up (ugh async), I've been running this code in a REPL. The REPL is buggy, and so I have to use ctrl+Z to kill it a lot. After killing the process, it seems the child process is still running...

lsof -i :54340
COMMAND   PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
python  52650  max    3u  IPv4 20606263      0t0  TCP localhost:54340 (LISTEN)

Now if I try to run it again, I get an error saying the port is already in use. Why doesn't the child process die with the parent?

役に立ちましたか?

解決

Different operating systems handle child processes differently. I usually add in handler like this:

['SIGINT', 'SIGHUP', 'SIGTERM'].forEach(function(signal) {
    process.addListener(signal, gracefulShutdown);
});

gracefulShutdown should do things like close sockets and quit processes (process.stop())

OH... And I just reread your question. ctrl-z pauses a process, it doesn't kill it. If you use fg or bg, it will bring the process back into the foreground/background. To quick the REPL, use ctrl-c twice.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top