Bash pipe & sigterm

I have a bash script "script" that looks something like this:

#!/bin/bash

cmd1 | cmd2 | cmd3

When I do kill script(more precisely, when I do a "stop script" in supervisord), not all cmd * will be killed. How can I make sure they are complete with the script that spawned them?

+5
source share
3 answers

Supervisord has a killasgroup parameter (false by default) that determines whether stop / end signals should be propagated to child processes.

[program:script]
command=script
killasgroup=true

https://github.com/Supervisor/supervisor/blob/master/supervisor/process.py#L354

+5
source

, supervisord, pkill -P, . ( ssh-).

$ pstree -a -p 1792
sshd,1792
  ├─sshd,27150
  │   └─sshd,27153
  │       └─zsh,27154
  │           └─test.sh,27325 ./test.sh
  │               └─cat,27326
  └─sshd,27182
      └─sshd,27184
          └─zsh,27185
              └─pstree,27357 -a -p 1792

script test.sh pid 27325, pstree -a -p 1792 ( sshd pid 1792)

pkill -TERM -P 27325:

$ pstree -a -p 1792   
sshd,1792
  ├─sshd,27150
  │   └─sshd,27153
  │       └─zsh,27154
  └─sshd,27182
      └─sshd,27184
           └─zsh,27185
              └─pstree,27387 -a -p 1792

stackoverflow: fooobar.com/questions/7031/...

+2

SIGTERM trap.

, - - .

However, traps will run in async mode if the shell is idle.

#!/usr/bin/env bash
trap 'kill 0' TERM
( cmd1 | cmd2 | cmd3 ) & wait

kill 0 sends SIGTERM to all processes in the current process group.

NOTE. I'm talking about bash here.

0
source

All Articles