question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

exec('nohup node some.js &')

See original GitHub issue

Not sure if its a bug rather question.

  • node - 5.10.0
  • shelljs - ^0.6.0
  • os - OS X 10.11.4

Can I do something like:

// index.js
require('shelljs/global');
const path = require('path');
const target = path.join(process.cwd(), process.argv[2])
exec('nohup node ./watcher.js ' + target + '&');
// watcher.js
const fs = require('fs');

function watch(dir) {
  fs.watch(dir, (event, filename) => {
    if (filename) {
      // do something
    }
  });
}

watch(process.argv[2]);
node src/index.js ./test/fixtures

I expect the index.js to terminate and on ps -ef | grep watcher to find the process corresponding to watcher.js listed

In practice it doesn’t. Is it by design?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
nfischercommented, Feb 26, 2017

@kevin-smets thanks for following up on this issue.

However, this is not a ShellJS bug. child_process.execSync() and child_process.exec() both have the same issue, so there’s nothing we can do here (our .exec() is built upon those).

For this specific example, here’s some thoughts:

  • Use child_process.fork() instead. This is much safer and more robust for executing other scripts asynchronously
    • This is cross-platform. The example above is unix-only (depends on nohup and &)
    • This works even if your version of node is installed under a different name (e.g. nodejs on Ubuntu)
    • You’re safe from shell expansions you might not expect and other security risks
  • Why do you really want to start a process without waiting for it to finish?
    • If you really want to start a job and continue using your shell, why not invoke it yourself with a & on the end?
    • If you want your program to start a job in the background while it runs, async (like .fork() above) should be sufficient
    • When your program has finished doing useful work, it can clean up responsibly and kill the child process with .kill(). Otherwise, you’ve got processes running around on their own on your system (yikes!)
1reaction
mncharitycommented, Mar 6, 2018

Another option is:

const { spawn } = require('child_process')
spawn( command, args, { detached:true, stdio:'ignore' }).unref()

Or stdio:'inherit', for terminal io.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to run Node.js as a background process and never die?
nohup means: Do not terminate this process even when the stty is cut off. > /dev/null means: stdout goes to /dev/null (which is...
Read more >
Starting a Node.js process using UNIX's boilerplate tools
If I try to open a new terminal, it automatically runs two processes, the first is iTerm's internal process, and the second one...
Read more >
Runing Node JS Server In Background - Iulian Popa
The nohup command will run commands in background, in this case, it will start our Node JS HTTP web server in background and...
Read more >
How to run a node.js app as a background service
The nohup is another Command Line Interface Tool which can be used to run a node.js app as a background service. Run the...
Read more >
How to use nohup to continue to run a command after the user ...
But, I would like to continue to run the program after I logout from SSH. When I run nohup <installation_dir>/nodejs/bin/node <js_program.js> > ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found