I'm trying to restart a certain child_process
by the name of serverSpawnProcess
. I can get it to stop, but I can't start it again.
serverSpawnProcess.stdin.write('stop\n');
serverSpawnProcess = null;
setTimeout(function() {
serverSpawnProcess = spawn('java', [
'-Xmx512M',
'-Xms512M',
'-jar',
'server_files/minecraft_server.jar',
'nogui'
]);
response.send('r');
}, 10000);
is how I thought I'd go about it, but it only stops the server, it won't spawn it again. Node.js 5.3.0 if that helps.
EDIT For the sake of anyone else looking for how to do this, I ended up putting the spawn into a function, which I call like so
serverSpawnProcess.on("close", function() {
response.send('r2');
// Wait for process to exit, then run again
startServer();
});
I'm trying to restart a certain child_process
by the name of serverSpawnProcess
. I can get it to stop, but I can't start it again.
serverSpawnProcess.stdin.write('stop\n');
serverSpawnProcess = null;
setTimeout(function() {
serverSpawnProcess = spawn('java', [
'-Xmx512M',
'-Xms512M',
'-jar',
'server_files/minecraft_server.jar',
'nogui'
]);
response.send('r');
}, 10000);
is how I thought I'd go about it, but it only stops the server, it won't spawn it again. Node.js 5.3.0 if that helps.
EDIT For the sake of anyone else looking for how to do this, I ended up putting the spawn into a function, which I call like so
serverSpawnProcess.on("close", function() {
response.send('r2');
// Wait for process to exit, then run again
startServer();
});
Share
Improve this question
edited Dec 24, 2015 at 16:59
gmemstr
asked Dec 21, 2015 at 16:48
gmemstrgmemstr
1651 gold badge2 silver badges9 bronze badges
1
- Have you taken a look at the error event from the newly spawned process? What does that say? – Ross Commented Dec 21, 2015 at 17:45
4 Answers
Reset to default 3The way in which I have acplished this is as follows. Say I have a camCmd
which allows me to run a DSLR camera as my webcam, but it crashes periodically. The goal is to restart it automatically without disrupting the stream. The code below does exactly that.
const { spawn } = require('child_process')
const camCmd = `gphoto2 --stdout --capture-movie | \
ffmpeg -i - -vcodec rawvideo -pix_fmt yuv420p \
-loglevel error -f v4l2 /dev/video1`
const respawn = spawned => {
spawned.on('close', () => {
respawn(spawn(camCmd, { shell: true }))
})
}
respawn(spawn(camCmd, { shell: true }))
Very simply it's a recursive function with an argument of whatever you're spawning. Which, once closed calls itself with the newly spawned arg.
So my guess is after 10 seconds timeout your minecraft server is still running (according to docs it saves some data to disk and stuff) and when you try to run another instance it fails due to port still being used.
Try it like this:
serverSpawnProcess.stdin.write('stop\n');
serverSpawnProcess.on("close", function() {
// Wait for process to exit, then run again
serverSpawnProcess = spawn('java', [
'-Xmx512M',
'-Xms512M',
'-jar',
'server_files/minecraft_server.jar',
'nogui'
]);
});
Also as https://stackoverflow./users/1139700/ross suggests, add a listener to error event so you know exactly what your problem is.
serverSpawnProcess.on("error", function(err) {
// ugh, something went wrong
console.log("Server error:", err);
});
Under what conditions are you trying to restart it? When it errors out, or on a specific condition? If the former, check out: http://schier.co/blog/2013/01/06/restarting-workers-in-a-nodejs-cluster.html
I was able to set up my server to auto-restart on errors by nesting my server in the following method, modified from the link above:
var cluster = require('cluster');
if (cluster.isMaster) {
cluster.fork();
cluster.on('exit', function(deadWorker, code, signal) {
// Restart the worker
var worker = cluster.fork();
// Note the process IDs
var newPID = worker.process.pid;
var oldPID = deadWorker.process.pid;
// Log the event
console.log('worker '+oldPID+' died.');
console.log('worker '+newPID+' born.');
});
} else {
// server code is initialized here
}
var serverSpawnProcess = spawn('java', [
'-Xmx512M',
'-Xms512M',
'-jar',
'server_files/minecraft_server.jar',
'nogui'
]);
serverSpawnProcess.on('exit', function (code)
{
console.log("exit here with code: ", code);
});
serverSpawnProcess.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
serverSpawnProcess.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});