SSH screen: how to back to the original window after Ctrl+a+c? - ssh

I'm running codes in session run. When I'm at this session, I occasionally create a new window by Ctrl-a c (in fact, I want to use Ctrl-a d to detach this session). Then I use Ctrl-a d to detach the session. But when I tried to enter the session run again to check the running progress using screen -r run, I found that I couldn't enter the initial session which runs my codes, instead, entered the created new window in which nothing runs. The screen -ls shows only one session named run. But top shows that all commands are still running. How can I enter the initial session run which runs my codes?

Related

How open nested screen session with screen command linux

I want to open multiple sessions in one main session with a screen command, and I can do that after opening the main session and then press on the keyboard (CTRL + A + c) but I don't want this way I want to do this using the command line only because I create a script to open the main session with multiple sub-session, so how can I achieve this. And thanks in advance :)
CTRL+A+C doesn't create a new screen session. It creates a new window within the current session.
(With a little environment trickery, you could create a nested screen session within an existing screen session, where the outer screen session and the inner screen session could each have multiple windows, but that's probably not what you want to do.)
Each window within a screen session runs a command. By default, that command is your login shell. Thus you can have multiple shell processes running simultaneously and switch between them.
From any of those shells, you can create a new window under the current screen session using the screen command. It uses the $STY environment variable to recognize that it's inside an existing session, which tells it to create a new window rather than starting a new session. You could also type, for example, screen vi to launch a new window running the vi editor.
All this is covered in the screen documentation (info screen or man screen).
(Personally, I used screen for a long time, but then switched to tmux, which I find to be more flexible, more powerful, and easier to control from the command line. If you haven't invested a lot of time learning screen, you might consider switching. Or not; it's up to you, of course.)
You can do this using by by putting the following in your ~/.screenrc (or using a custom .screenrc file):
$ cat ~/tmp/screenrc_custom
...
<content of ~/.screenrc>
...
screen -t csh 0
screen -t csh 1
screen -t csh 2
$ screen -c ~/tmp/screenrc_custom

WSL terminal forgets to update and loses focus

I'm trying to use the WSL console in Windows 10 (version 1909) for some development work, but I find that often running processes will simply appear to have hung, but when I send a keystroke to the terminal it will update back to the bash terminal (as if had completed all along but hadn't updated).
I am running the latest version of the debian package from the store.
Is there anyway to make this problem go away or am I wasting my time trying to use the default terminal?
Just a hunch, but I am guessing you are ending up in select mode. If you click anywhere in the terminal the title of the window will change to be prefixed with "select" and the terminal output will freeze so that you can select text without it changing while you try to highlight it. Pressing any key will cause you to exit select mode.
If this bothers you you can turn it off by right clicking on the title bar, clicking properties, and disabling quick-edit mode. Alternately, might I recommend the new Windows terminal, which handles selection and the clipboard in a more intuitive way.

Scripting GNU screen

I've been trying to come to terms with scripting in GNU Screen. Upon reading the man pages and a few other examples, I understand that we can use the at command or the -X argument for sending commands to screen sessions.
Let me explain my situation. I need to write a script that runs from with in an existing screen session. This script should create new windows, set their titles, browse to a specific directory and run a program in each of these windows.
The problem with the at command is that I can only send one command at a time. When I create a new window using at command, it will not be possible for me to obtain the window number of that newly created window. Because of this, I will not be able to send any more commands to that new window. How can I retrieve the window number of this new window?
On creating a new screen window you can specify a name with -t (e.g. -t test) and send commands via -Xto this specific screen by using the additional parameter -p (e.g. screen -p test -X eval 'yourcommand --yourparameter')

How can I create a nested screen session?

What do I need to put in my .screenrc so that I can create nested sessions (ie: a screen within a screen)?
At the moment, if I run screen again from within an open screen tab, instead of opening a new screen session within that tab, it just opens a new tab (ie: same effect as if I had pressed -C)
I want to get an outer screen session and an inner screen session - How do I do this please?
My .screenrc is empty except for the following single line to show the list of open tabs:
hardstatus alwayslastline "%{=w}%{G} %{b}%w"
This is a very old question, but for reference:
Start a MAIN screen session:
screen -S main
Start a second session (detached):
screen -dmS SomeName
From the tab you want nested session:
screen -r SomeName
Use and extra a for commands to nested screen, i.e. ^a-a-c to create a tab in the nested screen.
Don't know if what you mean is to start some nested sessions from your screenrc or to set something in your screenrc so you can start nested sessions from the colon prompt.
For the first, I use a separate screenrc file, .screenrc.main, which contains
source ~/.screenrc
screen -t <window name> screen -S <nested session name> -e^jj <command>
I think the "new session name" part is necessary to get a new session. I use a different command character for the nested sessions. Also I am using a separate file because I don't want the command to be executed for the nested sessions. Then from command line I do
DISPLAY= screen -S <top session name> -c .screenrc.main
to start the outer session.
For the second, I just type in the screen -t line above in the colon prompt.
From what I read in your question, it seems that you may be confused how to operate the nested session?
CTRL+a c will create a new 'tab' in the outer screen session.
CTRL+a a c will create a new 'tab' in the session 1 deeper than the previous
CTRL+a a a c creates a new tab in the session 1 deeper than the previous
I have had situation with 3 layers, where I SSH into my work box, then from there into a proxy and from there onto the server I want to work on, each time opening a screen session to be able to resume later.
Without a hardstatus showing the full hostname, it is very easy to get lost, very easy indeed!
I don't know about editing your screenrc file, but what works for me is sshing into the same computer then starting a new screen instance. Hackish workaround, but it does work...
screen
ssh 127.0.0.1
screen

Arrow keys stop working when using less in a gnu screen session

Sometimes when I am using less within a screen tab, the arrow keys display ^[OA, ^[OB, ^[OC, and ^[OD instead of doing what I want them to do. Is there something I can do to fix this and gain control of less again?
enter !reset at the less prompt
I have found that reset from within screen does not solve the problem sometimes, as it's the outer client/shell whose state is actually confused and screen captures the control characters from reset and prevents them from reaching the outer client. In this situation, I have to detach my session (Ctrl+a, d), run reset, then attach to the session again (screen -r).
If it happens from time to time, it seems, that some application (e.g. cat or less a binary file) shatters your console by sending it control characters. You need to run reset command from command line to recover.
Otherwise you have to trick your terminal application. I suggest you to use CryptoTerm which allows you to define custom key mappings.
Another thing to check is your TERM variable. In my case I ssh into a Linux box and run less inside screen - the TERM variable was set to 'screen' - which breaks arrow keys. It works perfectly if I run less this way:
TERM=xterm less <file>