3

I want to start a new gnome-terminal window with some command in multiple tabs. Based to this question:

gnome-terminal --tab -t "FOO" -e "/bin/zsh -i -c 'cd $(pwd); zsh -i'" \
               --tab -t "XYZ" -e "/bin/zsh -i -c 'cd $(pwd); tail -f /var/log/dmesg; zsh -i'"

The problem I'm facing is that after I CTRL-C the blocking tail -f tab just quits and disappears. Though, I'd like it to stay open and possibly inspect the past output.

Somehow my guess is that some parent gets the SIGINT and quits it all together instead of sending it to the tail command.

For instance, a simple sleep works just fine:

gnome-terminal --tab -t "FOO" -e "/bin/zsh -i -c 'cd $(pwd); zsh -i'" \
               --tab -t "XYZ" -e "/bin/zsh -i -c 'cd $(pwd); sleep 3; zsh -i'"

This will keep both tabs open with a prompt.

2

Use zsh's trap builtin (the trap builtin is also present in bash):

gnome-terminal --tab -t "FOO" -e "zsh" \
--tab -t "XYZ" -e "zsh -c 'trap \"zsh\" 2; tail -f /var/log/dmesg'"

Or:

gnome-terminal --tab -t "FOO" -e "zsh" \
--tab -t "XYZ" -e "zsh -c 'trap \"zsh\" SIGINT; tail -f /var/log/dmesg'"

It looks like you're running this from sh. Some side notes:

  • zsh in your installation is in /bin (hence in path) already, so all you need to spawn a zsh shell is zsh;
  • zsh runs interactively by default, so probably all you need to spawn an interactive zsh shell is to run zsh;
  • zsh is smart enough to read pwd by default, so probably all you need to spawn an interactive zsh shell in the same working directory of your sh instance is to run zsh;

The problem, as you thought, is the SIGINT signal, which is propagated to all the processes in the same process group of the process receiving the signal.

Using trap 'zsh' 2 / trap 'zsh' SIGINT the current zsh instance is set to spawn another zsh instance upon the reception of a SIGINT signal, which bypasses the problem.

You may also make the zsh instance "immune" to multiple signals by listing them space-separated in the arguments:

trap 'zsh' 1 2 3 [...]

Or:

trap 'zsh' SIGHUP SIGINT SIGQUIT [...]

You can print an ordered list all of all available signals by running kill -l:

% kill -l
HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH POLL PWR SYS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.