1

How can you suppress the the output resulting from kill when the target process is a parent of the current one?

I'm using the following function to exit out of any level of nested shells but keep the last one open:

function surface(){
    if [ $SHLVL -gt 1 ]; then
        target=$(pstree -p $$ | grep -m 2 sh$ | tail -1 | awk '{print $2}') &>/dev/null
        kill -1 $target &>/dev/null
    fi
}

But this still prints the message Hangup: 1 to the terminal.

Adding wait $target after kill fails, as wait gives the error: bash: wait: pid 1234 is not a child of this shell.

I also tried using disown, but that similarly fails because the target process is a parent of the current one.

Related:

How to suppress Terminated message after killing in bash?

How to shield the kill output

bash: silently kill background function process

Community
  • 1
  • 1
SnoringFrog
  • 1,479
  • 1
  • 16
  • 30
  • Try `trap "exit" 1` in the shell that is being killed. This will cause it to exit normally rather than abnormally, and its parent won't print anything. – Mark Plotnick Jan 14 '16 at 18:59
  • @MarkPlotnick the situations I use this in don't really leave that as a (helpful) option; I don't see far enough in advance that I'll want this to think t leave a `trap` behind me – SnoringFrog Jan 14 '16 at 20:47
  • 1
    Hmmm. You can put `shopt -q login_shell || trap "exit" 1` in every (bash) shell without any real loss of functionality. It'll install the trap only in non-login shells. – Mark Plotnick Jan 14 '16 at 20:57
  • @MarkPlotnick Not the kind of solution I was hoping for, but it would work if I don't find something better. Only works for bash, but the only other shell I use (fish) doesn't produce the unwanted output anyways – SnoringFrog Jan 14 '16 at 21:08
  • We may be able to offer more suggestions if we can see samples of what kind of commands are being run in the shells that you want to terminate, and their parent shells. Most of the solutions in the linked questions work only when the process that's killed has a parent that either knows the process may be killed or runs the process in the background and does a `disown`. – Mark Plotnick Jan 15 '16 at 16:55
  • @MarkPlotnick There isn't really any common theme to the commands being run, which is why I'm looking for a more general solution – SnoringFrog Jan 15 '16 at 20:51

0 Answers0