0

I'm in the terminal emulator. I want to open another terminal to run a specified command.

gnome-terminal -e "zsh -c 'pwd; zsh;'"

That runs pwd and zsh consecutively and successfully in another terminal. After pwd terminates, zsh launches and I can run other commands in that second terminal instance.

However, when a different app is launched instead of pwd, I can't end that long-running program properly.

For example when I type CtrlC to exit from node.js server in below command, it also closes the terminal. It seems that SIGINT is passed to first zsh with -c option.

gnome-terminal -e "zsh -c 'node server.js; zsh;'"

How can I fix this?

  • 1
    If I use zsh as in your code, I reproduce your problem, but bash does not have that problem; if you replace zsh with bash, it works fine for me. Would using bash be acceptable? – Zorgoth May 16 at 20:30
  • 1
    You can also use gnome-terminal -e "bash -c \"node server.js; zsh\"" so that you still have your favorite shell. – Zorgoth May 16 at 20:42
  • @Zorgoth Thank you. It works with bash but some commands that spawns another process(maybe by forking?) still exit when sigint is passed. – soonoo May 17 at 3:57
  • Script in answer of @HarshaD works in my case. – soonoo May 17 at 4:01
1

I tried this in bash

Apologies in advanced if it does not work.

I wrote a bash script named userInput.sh - this waits for user input and ends.

`

## trap ctrl-c and call ctrl_c()

trap ctrl_c INT

function ctrl_c() {

echo "** Trapped CTRL-C"

exit
}

read -p "Press any key to continue... " -n1 -s 

Now, when I run and press clt+c

gnome-terminal -e "bash -c './userInput.sh; bash;'"

userInput.sh exits and I still have the bash prompt.

Now I think in your case instead of waiting for user input, you are running node server.js like

## trap ctrl-c and call ctrl_c()

trap ctrl_c INT

function ctrl_c() {

echo "** Trapped CTRL-C"

exit
}

node server.js

Let's assume that you named the script 'runNodeJs.sh', then command would be

gnome-terminal -e "bash -c './runNodeJs.sh; bash;'"

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.