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?

It would be convenient in this case, because the oracle account does not have sudo privileges. So for instance, I'm in /some/really/deeply/nested/directory in oracle, and I have all kinds of special environment variables set up. But I need to get back into root to touch some system files.

Currently, I would drop out of the oracle shell to get back to root, run my root commands, then switch back into Oracle. The inconvenience is that I'm logged in from a fresh start, and I lose my working directory and environment.

That being said, is there any way to do what I'm asking?

  • 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 '14 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. – Avindra Goolcharan Dec 26 '14 at 20:08
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.

  • 1
    In zsh you can just run suspend. – Mikel Dec 19 '14 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 $$)). – Stéphane Chazelas Dec 19 '14 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. – Stéphane Chazelas Dec 20 '14 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). – Stéphane Chazelas Dec 20 '14 at 16:24
  • 2
    @nyuszika7h, $$ may be readonly, but IFS is not. Try IFS=0123456789; suspend in ksh for instance. – Stéphane Chazelas Dec 20 '14 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.

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

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.