I have two scripts. These are simplified. The root-script.sh
calls userscript.sh
:
root-script.sh:
#!/bin/bash
su - user1 -c "/user1path/user-script.sh"
user-script.sh:
#!/bin/bash
trap 'echo please use x for exit' 2
while x=0; do
read -p "enter x for exit:" answer
if [[ $answer = 'x' ]]; then
echo "exit now"
exit 0
fi
done
If I call user-script.sh it just works as it should:
enter x for exit:
enter x for exit: ^C_please use x for exit
^C_please use x for exit
x
exit now
If I call root-script.sh
as root
and enter a Ctrl-C I get
enter x for exit: ^C
Session terminated, killing shell... ...killed.
Than I get back the root-prompt but the prompt is blocked. With ps I don't see the root-script, only the user-script. If I kill the user-script the root-prompt is usable again.
How can I prevent the root-script-user-script-construction from hanging after SIGINT? Means for me
- Exiting
root-script.sh
anduser-script.sh
or the root-script-user-script-construction should work as same as
user-script.sh
- bash-version: 3.2.51(1)-release (x86_64-suse-linux-gnu)
- os-version: sles11 3.0.93-0.8-default
I just tested:
verses
and found that the former could not be terminated via SIGINT but the latter could. My guess is that perhaps su -c opens the user's shell to run the command passed by -c, and that is what will catch the SIGINT and terminate - but your script only captures the SIGINT in a subshell - which is maybe passed SIGTERM by the parent shell's SIGINT handler.
Hopefully that works for you.
EDIT:
confirms that the command is run with the user's shell.
Perhaps you will find
To be a more elegant solution. I think it will work but have not tested it. exec will replace the current shell process with your script's process, though, so I think it should work.
EDIT 2:
Looking back at your question, you just need the trap in the root script, I think. Or maybe:
If you want to completely inherit the script's trap behaviour.