4

I have created a node script which should run as a background process. Right after the script starts running run I need to get some user data (username & password).

After getting the user data I wish to close the terminal but the process should continue running in the background.

To complicate things, the node script is being built with pkg lib and will be started as an .exe

pkg on NPM

Here is a the code that will be executed:

async function init() {
  try {
    await fetchConfig();
    const credentials = await getCredentials(); // this one prompts the request for user input
    watchForNewFiles(credentials); // that one should use the credentials and continue running in the background.
  } catch (e) {
    console.error(e);
  }
}

init();

1
  • Show the code plz... :) Commented Apr 6, 2021 at 1:01

2 Answers 2

1

Here's a solution:

// Neccesary libs
let fs = require("fs");
let child_process = require("child_process");

// Filename should end in .bat
let filename = "__tmp.bat";

// Main function, call this to prompt the values.
function prompt() {
// Write batch script
// Prompts 2 variables & writes them as JSON into the same file
fs.writeFileSync(filename,`@echo off
set /p Var1="Name: "
set /p Var2="Password: "
echo {"name": "%Var1%", "password": "%Var2%"} > ${filename}
exit`);

// Execute the script in a cmd.exe window & write the result into stdout & parse it as JSON
let result = JSON.parse(child_process.execSync(`start /w cmd /c ${filename} && type ${filename}`));

// Delete the file
fs.unlinkSync(filename);
return results;
}

// Example usage:
console.log(prompt()) // prints { name: 'hi', password: 'world' }

Tested as a node14-win-x64 pkg binary, works perfectly.

Sign up to request clarification or add additional context in comments.

9 Comments

Can you please explain what's going on here.
We write a batch file that batch file asks you for a few variables. (set /p) Those variables get written into the same file when the batch file gets executed. (echo ... %Var1% ... > ${filename}). Then we start the batch file in a cmd.exe window (child_process.execSync('start /w cmd /c ${filename}) and read the contents the batch file has written (type ${filename}) and parse it as JSON (JSON.parse). Then we delete the file (which contains the output, which isnt needed anymore, fs.unlinkSync(filename)). We need to do that because CMD.exe doesnt do that natively.
But currently the original script is prompting the request for the user input
Isn't that what it's supposed to do? Am I missing something?
Currently the script asks the user for username and password. And then runnung while keeping the terminal window open. I would like to keep the same behavior, but instead of keeping the terminal windown open, I would like to close it but keep the process running in the background.
|
0

AFAIU this is not possible to do from inside node.js's running code by itself.

You have to use some kind of tool to put the node.js program in the background.

This answer lists several tools for this purpose.

This one is also useful.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.