Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not functional on Docker Alpine #38

Open
Juriy opened this issue Jan 20, 2022 · 3 comments
Open

Not functional on Docker Alpine #38

Juriy opened this issue Jan 20, 2022 · 3 comments

Comments

@Juriy
Copy link

Juriy commented Jan 20, 2022

Hello,

Trying to use this module for killing a process tree in Docker that is built on Alpine image. Unfortunately, the module fails to create a process tree, since ps does not support some of the flags that the module is relying on: --no-headers and --ppid. This results in a process returning non-zero code. Module interprets non-zero as "no child processes" and swallows the error silently.

I was trying to find a way to change the command in buildProcessTree so that it simulates the same output. Removing header is easy - we can pass the names of the columns with -o, removing the header completely:

/usr/src/app # ps -opid=""
    1
   23
   49
   67

But lack of --ppid filter support making it slightly more challenging. My Linux knowledge is not enough to tell if manually filtering by ppid in the following command would do the trick:

/usr/src/app # ps -opid="" -oppid=""
    1     0
   23     1
   49     0
   68    49

If that's the case, it would probably be the most portable version (except for the cases when ps is not present at all as described in #36

If there's an ambition to rewrite this part of the code, I am happy to collaborate 🙂

@m0ngr31
Copy link

m0ngr31 commented Oct 5, 2022

@Juriy this is what I was able to whip up in my own project:

export const killChildren = (pid: number) => {
  const children = [];

  try {
    const psRes = execSync(`ps -opid="" -oppid="" |grep ${pid}`).toString().trim().split(/\n/);

    (psRes || []).forEach(pidGroup => {
      const [actual, parent] = pidGroup.trim().split(/ +/);

      if (parent.toString() === pid.toString()) {
        children.push(parseInt(actual, 10));
      }
    });
  } catch (e) {}

  try {
    kill(pid);
    children.forEach(childPid => kill(childPid));
  } catch (e) {}
};

@TheThing
Copy link

TheThing commented Mar 2, 2024

thanks @m0ngr31 for that code sample. I ended up stealing it and using it in my project (WTFPL). Hope you don't mind me adapting it into my library and re-licensing as such?

@m0ngr31
Copy link

m0ngr31 commented Mar 2, 2024

thanks @m0ngr31 for that code sample. I ended up stealing it and using it in my project (WTFPL). Hope you don't mind me adapting it into my library and re-licensing as such?

Go for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants