-2

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
NotYourFox
  • 35
  • 5
  • 2
    Probably a better solution is to just return a result, and have the parent decide on their suicide based on the return value. – tripleee May 26 '23 at 06:21
  • Why the parent is `zsh`? Are you killing your login shell? – Diego Torres Milano May 26 '23 at 06:25
  • @DiegoTorresMilano no, I'm not running the killer script straight in zsh, I'm running the script which calls the killer script. Sorry for the confusion, I forgot to clarify it. Now edited. – NotYourFox May 26 '23 at 06:45
  • @tripleee yeah, I've thought about that, but I decided to try making it easier to just call the dependency resolver in my script in one command and not use any other statements (like checking the return code) every time I need it – NotYourFox May 26 '23 at 06:52
  • 2
    I don't understand why you are tagging your question zsh **and** bash. After all, you should know what language you are programming this in. BTW, in both bash and zsh, the parent process should be available in the variable `PPID`. No need to use a `ps`. Aside from this, it is in general a bad idea to kill your parents - in real life and in programming. If your script really depends on a certain main script, this smells like a need for redesign the interaction between the processes involved. – user1934428 May 26 '23 at 07:40
  • @user1934428 as I said, I'm doing this in bash environment, but have a problem with zsh involved in it. That's why I tagged them both. About the design - I, again, just wanted to make a custom dependency resolver for my scripts. I have chosen this design since, as I said earlier, I wanted to use it only as a single command in my scripts, without any checks afterwards, leaving the termination of the process to the dependency resolver. Also, thanks for an advice with PPID, I didn't know that. – NotYourFox May 26 '23 at 08:03

1 Answers1

1

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).

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134