Search code examples
linuxbashzshkill

bash/zsh: kill parent process and suppress the output


I'm kinda new to bash and I'm trying to do a bash script myself that can check and install specified packages if they are not present in the system. It should be called in some other script, and if the dependencies have not been resolved, it should kill the parent script so it won't continue executing. Now here's the minor, but annoying issue. I'm calling my main script (which then calls the dependency checker script as a subprocess, which may or may not kill it) from zsh (the main and killer script shebangs are #!/bin/bash), and when I kill the process, it outputs

zsh: terminated  parent

Here is how I do it:

#!/bin/bash

kill_parent () {
    ppid=$(ps -o ppid= $$)
    kill $ppid
}

# other code
kill_parent
# more code

I have found some articles on how to resolve this in bash, some of which were of literally the same issue, for example

and there were some ideas like using wait, disown, SIGINT (which does not kill the process at all), stream redirection and other things. But for me, I've tried all of that but the output is still there. I suppose, the problem is that it is zsh that outputs the message, hence, trying to do redirections in bash script does not help.

I've also tried to change shebang to

#!/bin/zsh

but running it in zsh outputs on kill

kill_parent:kill:2: illegal pid:  242038

Solution

  • As mentioned in the comments killing the parent is not the best way to go.

    You can do something like this and it

    #! /bin/bash
    # parent
    
    ...
    check_dependencies || exit
    ...
    

    no need to kill it. Providing that check_dependencies exits with a non-zero value (you can specify different return values for different conditions).