If I start a terminal (any terminal, for example urxvt) like urxvt -e sleep 5, then a new terminal is launched but after 5 seconds the terminal closes, because the sleep program has ended. How can I start a terminal with a program on the command line, but have the terminal stay alive after that process has ended?

In practice, what I'd actually like to do is urxvt -e tmux new-session top, which opens urxvt with a tmux session that is running top. But when I press q, top ends which also causes tmux and urxvt to end as well. I'd like when I exit top for me to be taken to a shell within tmux.

  • 3
    Not a duplicate. That post has to do with keeping the process alive when the terminal exits. I'm asking about keeping the terminal (and tmux) alive when the process exits. – user779159 Sep 23 '16 at 13:28
  • It strikes me that urxvt -e sleep 5 and a shell please would be better implemented as ( sleep 5 && urxvt ) & – roaima Sep 23 '16 at 18:20

The terminal (tmux) closes when it's executed the command you told it to execute. If you want to execute top and then an interactive shell, you need to tell it to do that. Combining commands is the job of the shell, so run an intermediate shell (which isn't interactive) and tell it to run the two commands in succession.

urxvt -e tmux new-session sh -c 'top; "$SHELL"'

One possibility:

gnome-terminal -e sh -c "sleep 5; bash"

The point is to run the shell (bash in this case) after your initial command will finish. Unfortunately simple gnome-terminal -e 'sleep 5; bash' is not possible because the whole sentence sleep 5; bash is interpreted as one command.

  • urxvt doesn't seem to start for me when the -e flag contains something in quotes. So nothing even launches for me. urxvt -e 'sleep 3' doesn't launch any urxvt, but urxvt -e sleep 3 does. – user779159 Sep 23 '16 at 13:44
  • 1
    @user779159 I've tested on gnome-terminal and it works. I will try urxvt later on. – jimmij Sep 23 '16 at 14:01
  • rxvt -e takes a list of arguments: the command to run and its arguments. Gnome-terminal supports this but for some reason they renamed the option to -x and used a different semantics for -e. – Gilles 'SO- stop being evil' Sep 24 '16 at 23:34

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