Search code examples
javascriptnode.jswindowsregistry

Run Windows Node.js script in background when the user logins by setting a Run key


I'm running on Windows Server 2012R2.

I have a node.js script which I want to run when the user performs logins.

To do this I'm setting the command to run in the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run registry path.

I set this command:

/path/to/node/node /path/to/node/script args

This works fine, but it will spawn a terminal showing the command output, while I'd like to run this command in background and detached from any command terminal.

I tried to replace the previous command with: start /b /path/to/node/node /path/to/node/script args

but in this case there is no evidence that the script was even started.

I also tried to wrap the following command in a .bat script:

start \b node script args

set the name of the script in the Run key: in this case I can see terminal flash but then the script is not running anymore (I suppose that the script is executed but then it is stopped as soon as the parent process is terminated).

I want to avoid to convert the script in a windows service, as long as a simpler solution is possible.

A solution not using the Run registry key is also fine, as long as it fulfils my requirement (run a script in background when the user logins).


Solution

  • Actually it's pretty easy , use forever.js module

    after installing the module use

    "forever start main.js"

    It will start running as your background process