Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8404239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:32:56+00:00 2026-06-09T22:32:56+00:00

Is it possible to make sure that processes spawned with node.js child_process will be

  • 0

Is it possible to make sure that processes spawned with node.js child_process will be killed when the parent is killed?

Here’s an example script

var spawn = require('child_process').spawn;
var log_tail = spawn("tail", ["-f", "/dev/null"]);

setInterval(function() {
  console.log('just chilling');
}, 10000);

If I look at the process tree I see this:

$ ps faux
ubuntu    9788  0.0  0.0  73352  1840 ?        S    15:04   0:00  |   \_ sshd: ubuntu@pts/7  
ubuntu    9789  0.0  0.2  25400  6804 pts/7    Ss   15:04   0:00  |       \_ -bash
ubuntu   10149  1.0  0.2 655636  8696 pts/7    Sl+  15:07   0:00  |           \_ node test.js
ubuntu   10151  0.0  0.0   7184   608 pts/7    S+   15:07   0:00  |               \_ tail -f /dev/null

Then later I need to stop that script and can’t ctrl-c (say my ssh connection is lost)

$ kill 10149
$ ps faux
ubuntu   10151  0.0  0.0   7184   608 pts/7    S    15:07   0:00 tail -f /dev/null

You can see that the tail process is still running and has become detached from any parent.

Is there a programatic way to kill a spawned process if the spawner is killed?

Is there an option I can pass to spawn or should I use a different method or do I need to catch the kill signal (and will that work for a kill -9)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:32:57+00:00Added an answer on June 9, 2026 at 10:32 pm

    Not very labor friendly, but this serves as the template I use:

    var spawn = require("child_process").spawn;
    var workers = [];
    
    var killWorkers = function() {
      workers.forEach(function(worker) {
        process.kill(worker);
      });
    });
    
    process.on("uncaughtException", killWorkers);
    process.on("SIGINT", killWorkers);
    process.on("SIGTERM", killWorkers);
    
    var new_worker = spawn("tail", ["-f", "/dev/null"]);
    workers.push(new_worker);
    

    I primarily use this for killing workers in a cluster when the master process dies. Ed: Obviously, you could just call killWorkers from anywhere, not just crashes or interrupts.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do you know if it is possible to make sure that a JavaScript script
Is it possible make some handler that will do something when user shutdown computer
Is it possible make an activity, that will lock the android device? That device
Using GNU make, is it possible to create a set of targets that will
Is it possible to make sure my thread reacts on whenever there is network
I think it's not possible, but I want to make sure.
Wanting to make sure I'm using classes properly. The main script accepts this from
Possible Duplicate: Make error: missing separator I am so stressed out by this silly
Possible Duplicate: Make iPhone app paid version replace free version on install from app
is possible to make own help message or attach own event on help option

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.