Running Electron application as Linux daemons

The node-windows library can only work on a Windows machine. This is just one of the libraries available in the npm registry. You can use any of the available process manager libraries to make your application a background service. In Linux machine, this can be achieved in several ways. Let's use the node-linux library to run the node script as background from your Electron application. Install the dependency from the npm registry:

npm install --save node-linux

We can use the same server script as an example. Once the dependency is installed, the script can be started as service with the following code:

const { Service } = require('node-linux');
service = new Service({
name: 'Node JS Server',
description: 'A simple node js server',
script: path.join(__dirname, 'server.js'),
env: {
name: 'HOME',
value: process.env['USERPROFILE']
}
});
service.on('install', () => {
service.start();
});

service.install();

The code is similar to the code snippet that we discussed in the preceding section. Also, you don't have to use this library. You can even use the shell script or batch file to start a node script as a service.

There is an alternative version of this library available for Mac called node-mac that can be used to create and manage background services. The code is similar, but importing the library is different, depending on your platform. Use node-windows, node-linux, or node-mac based on the target operating system. Mac background services can be monitored in activity monitor:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.58.196