coreybutler / node-windows

Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
Other
2.83k stars 356 forks source link

My service is stopped but the program is still running #337

Closed pols63 closed 1 year ago

pols63 commented 1 year ago

Hi:

When I stop my service, the "node.exe" is still running.

This is my installer file:

installer.js

const NodeWindows = require('node-windows').Service
const path = require('path')

const command = process.argv[2]
if (!['install', 'uninstall'].includes(command)) throw new Error(`Parámetro no válido`)

const service = new NodeWindows({
    name: 'Habacuc Node',
    description: 'Habacuc Framework in Node',
    script: path.join(__dirname, 'service.js'),
})

switch (command) {
    case 'install':
        service.on('install', function () {
            service.start()
        })
        service.install()
        break
    case 'uninstall':
        service.uninstall()
        break
}

And this is my script:

service.js

const { spawn } = require('child_process')
const path = require('path')

const child = spawn('npm.cmd', ['start'], {
    cwd: path.join(__dirname, '..'),
    env: [{
        name: 'TZ',
        value: 'America/Lima'
    }]
})

child.on('error', (...args) => {
    console.log(args)
})

child.on('close', (code) => {
    console.log(`El programa finalizó con código ${code}`)
    process.exit()
})

As you could see, I execute "npm start", this is the command to execute:

npx ts-node -r tsconfig-paths/register --project tsconfig.json .

In the wrapper.log, I get this message:

2023-02-09 17:47:51 - SIGINT to 5192 failed - Killing as fallback
2023-02-09 17:47:51 - Stop exception
Message:Access denied
Stacktrace:   en System.Diagnostics.Process.Kill()
   en winsw.WrapperService.StopProcess(Int32 pid)
   en winsw.WrapperService.StopProcessAndChildren(Int32 pid)
   en winsw.WrapperService.StopProcessAndChildren(Int32 pid)
   en winsw.WrapperService.StopProcessAndChildren(Int32 pid)
   en winsw.WrapperService.StopProcessAndChildren(Int32 pid)
   en winsw.WrapperService.StopIt()
   en winsw.WrapperService.OnStop()

After the service is stopped, the node.exe is still running, in this case, my web server is still listening.

My node version is 19.6.0

I hope your help. Thanks.

coreybutler commented 1 year ago

I realize this is a late reply, but there are two things I see. First, there is an access denied response, which means the user account running the service doesn't have permission to do what your script is trying to do. That's the first thing to change.

Second, do not use __dirname, as it changes based on the current working directory. See https://github.com/coreybutler/node-windows/wiki for more details.