-->

Electron dying without any information, what now?

2020-07-18 11:23发布

问题:

The app I'm building, when I compile it for distribution packing it with electron-builder, every now and then, dies, showing a blank screen and a disconnected devtools:

Any ideas what's going on or how to start figuring out what's happening here?

回答1:

Listen for the uncaughtException event and log any error that you get. This will give you insight into what is happening. Then perform any cleanup if necessary and relaunch the app if desired. This allows your app to "recover" from crashes if it is intended to be long-running.

//handle crashes and kill events
process.on('uncaughtException', function(err) {
  //log the message and stack trace
  fs.writeFileSync('crash.log', err + "\n" + err.stack);

  //do any cleanup like shutting down servers, etc

  //relaunch the app (if you want)
  app.relaunch({args: []});
  app.exit(0);
});

You can also listen to the SIGTERM event to see if your application is being killed off, and also gracefully shutdown servers, restart, etc.

process.on('SIGTERM', function() {
  fs.writeFileSync('shutdown.log', "Received SIGTERM signal");

  //do any cleanup like shutting down servers, etc

  //relaunch the app (if you want)
  app.relaunch({args: []});
  app.exit(0);
});