29

Let's say I start in my local account:

avindra@host:~>

then I switch to root:

host:~ #

Then I switch to oracle:

[ oracle@host:~]

Is there a way for me to drop back into the root shell (the parent), without logging out of the oracle shell?

This would be convenient, because the oracle account does not have sudo privileges. A typical scenario with oracle is that I end up in /some/really/deeply/nested/directory, and all kinds of special environment variables are set in particular ways.

Here comes the problem: I need to get back into root to touch some system files. Yes, I can log out of oracle to get back to root, but at the cost of losing my current working directory and environment.

Is there a way to "switch" to the parent shell using known conventions?

2
  • If you are using a gui desktop, you can just open another terminal window or tab or even switch to another virtual console. If not, use screen as stated in one of the answers.
    – Joe
    Dec 26, 2014 at 19:55
  • I'll look into screen, I haven't used it before. As for tabs, I prefer to use one tab for host. I find a workflow that entails multiple tabs per host to be cumbersome. I do a lot of work in clustered hosts / distributed systems, so even having one tab per node in the cluster can get confusing. In my mind, one tab = one discrete host. Dec 26, 2014 at 20:08

3 Answers 3

41

You can simulate a CTRL-Z (which you normally use to temporarily background a process) using the kill command:

[tsa20@xxx01:/home/tsa20/software]$ kill -19 $$

[1]+  Stopped                 sudo -iu tsa20
[root@xxx01 ~]# fg
sudo -iu tsa20
[tsa20@xxx01:/home/tsa20/software]$

bash just traps the CTRL-Z key combination. kill -19 sends SIGSTP to the process which is effectively the same thing.

12
  • 1
    In zsh you can just run suspend.
    – Mikel
    Dec 19, 2014 at 20:10
  • 3
    @Mikel, zsh or csh, tcsh, or bash, or the Bourne shell or ksh... (in ksh, suspend is an alias for 'kill -s STOP $$' (note the bug with the missing quotes around $$)). Dec 19, 2014 at 23:19
  • 2
    bash does not trap CTRL-Z, when you press CTRL-Z, your terminal emulator sends a ^Z character (0x1a) to the master side of the pty device. The line discipline of the pty driver then sends SIGTSTP to the foreground process group of the terminal. Until then bash is not involved at all. That SIGTSTP may cause the process group leader to suspend and then the wait() that bash does on it will return. Dec 20, 2014 at 16:23
  • 5
    Note that there's no guarantee that 19 will be the number of SIGTSTP. Use kill -s TSTP or kill -s STOP (TSTP can be trapped or ignored, STOP cannot). Dec 20, 2014 at 16:24
  • 2
    @nyuszika7h, $$ may be readonly, but IFS is not. Try IFS=0123456789; suspend in ksh for instance. Dec 20, 2014 at 16:26
3

Csh, bash, ksh, zsh, have a suspend builtin command (or alias, in ksh) that does exactly that. This command is mostly equivalent to sending a TSTP signal to the shell; bash and zsh do a bit of additional signal handler and juggling, and in these shells the suspend command works even if the shell is currently ignoring TSTP.

You can also send the signal to the shell manually with kill -STOP $$.

0

I would also suggest that you could use (install if needed) a program called [screen][1] that let's you have multiple terminal windows open. It was designed for TTYS so it works just fine without needing X. You can use keyboard shortcuts to switch between terminals and disconnect and logout, leaving your terminals still running, then log back in and reconnect to them.

I believe most Linux distributions have packages for this program, and I have used packages on Solaris for it. Worst case of course is you can install from source.

1
  • 1
    This might be more appropriate as a comment rather than an anaswer.
    – Barmar
    Dec 24, 2014 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .