13

This may sound like a contradiction in terms, but I've recently needed to use single user mode (aka maintenance mode) for some time (days!) while trying rebuild a broken RAID array.

While doing this, I've found myself wanting to use a second shell for such things as checking/editing the settings in various config files or looking at system logs while the main (and only) console is tied up executing some recovery process.

What would be the recommended command to launch a second shell on another tty?

Obviously, one method might be to use screen in tty1, but this doesn't have quite the ease of just switching sessions with Alt-F1, Alt-F2, etc.

StarNamer
  • 431
  • 1
  • 5
  • 15
  • 4
    `screen` is just as easy as that. Ctrl A P doesn't seem much trickier than Alt F1... – mveroone Mar 17 '14 at 08:04
  • Worth noting that apps like `screen` and `tmux` for TUI window management don't always do the trick. Sometimes you need an actual `ttyX` device, rather than a `pts`. – Parthian Shot Jun 24 '15 at 22:15

4 Answers4

14

There's a command specifically designed for this situation: openvt. Just run openvt from your shell on tty1 and you'll find a shell running on the first tty that didn't already have something on it (tty2 probably.) There are several options you might find useful; see the openvt man page.

Since this is not a login managed by getty, when you exit the new shell you won't see a login prompt come up. The tty will just go dead since there will be no processes running on it, but you will still be able to switch back and forth (Alt-F1 Alt-F2) and see what was on the screen when you exited the shell. The deallocvt command destroys ttys that are in this zombie-like state, returning to the original "Alt-F2 does nothing" state.

Originally these commands were called open and disalloc, but eventually somebody decided to change them because they were too generic and disallocate was judged to be "not a word".

In the old days it was common for the keyboard driver and init to be configured so that Alt+Up would run open, so it would act as a hotkey to spawn a shell on a new console. You may still find a remnant of that old configuration, commented out, in your /etc/inittab. (If you aren't using some fancy new init that doesn't have an inittab.)

There's a similar question here

  • 3
    Nice, I wasn't aware of `openvt`. +1. – jscott Mar 16 '14 at 23:03
  • This actually seems much easier that getty. I never realised it was already installed. – StarNamer Mar 21 '14 at 20:11
  • Just thought I'd report that I just spent over a week with a system in single user mode repairing a RAID array and `openvt` proved to be very useful. Thanks for bringing a new command to my notice. – StarNamer Mar 25 '14 at 23:18
11

You could spawn another getty for whichever ttys you want. Boot into single user mode, then start up a getty on tty2:

root@host:~# /sbin/getty 38400 tty2 &

You may now Alt+F2 over to the new tty. Repeat for additional ttys as needed. You could probably do something cleaner and just configure /etc/inittab to automatically handle this in single user mode.

jscott
  • 24,484
  • 8
  • 79
  • 100
  • This looks like what I'm looking for. I'll give it a try next time. – StarNamer Mar 16 '14 at 22:57
  • Tried it. It works, but since the login session isn't spawned from `init`, it doesn't get re-spawned if it dies. – StarNamer Mar 18 '14 at 10:22
  • Indeed, we're just dropping it into the background in the above example. If you require the full offering of a real vt, you're going to be hacking `/etc/inittab` and `/etc/rc1.d/S??single` at the very least. – jscott Mar 18 '14 at 10:27
2

You probably can do without an extra shell instance by using JOB CONTROL in your current shell. It is documented in the manpage of bash(1).

You can just suspend a task using the Ctrl+Z sequence by default, though it can be configured in your terminal in a different way, check the output of stty -a:

$ stty -a | grep susp
eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R;

You can check your jobs by issuing jobs:

# jobs
[1]   Stopped                 journalctl -f
[2]-  Stopped                 vim /etc/hosts
[3]+  Stopped                 tail -f /var/log/firewalld

Bring one of them to the foreground:

$ fg %3

Or resume it in the background:

$ bg %2

This method does not have many of the advantages of screen or tmux, but can be equally useful in some situations.

dawud
  • 15,096
  • 3
  • 42
  • 61
  • The point is that I don't want to suspend or interfere with the process running on the initial console. For example, when running `ddrescue` I don't want to keep interrupting and resuming it even though it's set up to do this.. – StarNamer Mar 16 '14 at 22:53
  • A couple issues with this. For one, he may not be using bash as his shell (could be using `csh`, `tcsh`, or `zsh`, for example), so mentioning its manpage would be inaccurate in that case. For another, this doesn't answer the question as asked. – Parthian Shot Jun 24 '15 at 22:13
1

Consider using terminal multiplexers such as tmux or screen.

Find out more about tmux here: https://github.com/tmux/tmux/wiki

Jonny Keogh
  • 103
  • 3
Marcin Kaminski
  • 243
  • 2
  • 10
  • The question mentions this already: "Obviously, one method might be to *use screen* in tty1*, but this doesn't have quite the ease of just switching sessions with Alt-F1, Alt-F2, etc.". – Cristian Ciupitu Mar 16 '14 at 22:26
  • As Cristian says, I'd already considered `screen` and felt other multiplexers were even more overkill. All I need is a simple second console. – StarNamer Mar 16 '14 at 22:56
  • Ah, sorry, I overlooked `screen` in your question. It was late. – Marcin Kaminski Mar 17 '14 at 00:07
  • How using a combination of 2 keys be "overkill" beside spawning new TTYs, and anchroing to them seperatly and such ? – mveroone Mar 17 '14 at 08:03
  • IMO, it's a strange definition of 'overkill' but hey, whatever works for someone, I'm fine with it ;) – Marcin Kaminski Mar 17 '14 at 10:11
  • The 'overkill' reference was more with regard to the windowing/split-screen capabilities of programs like `tmux`. After experimenting, I suspect that `screen` is probably more stable that `getty` in the single user environment. – StarNamer Mar 18 '14 at 10:20
  • @MarcinKaminski In some cases it actually really matters whether you're using a physical terminal device file (tty) v. virtual (pts). For example, by default X is usually installed to allow you to start an X server from the former but not the latter. – Parthian Shot Jun 24 '15 at 22:12