Scripting GNU screen - 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')

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

Running a command when a Button is pressed in a Cocoa GUI (Objective-C)

How can I have a shell command run when a user clicks a Button in my Cocoa GUI?
I would like to have a button that when pressed, runs the command caffeinate to keep the computer awake.
I do not know how to code this function and have googled.
You run command line commands via NSTask. You create an NSTask, set the path to the command you want to run, set up the arguments and then call the task. If you google how to use NSTask I'm sure you'll find some example code.
Here is the Apple Developer documentation for NSTask: https://developer.apple.com/documentation/foundation/nstask?language=occ

Porting CLI/GUI Windows program to OS X

I have a Windows program that has a GUI which also uses a command line interface (a cmd Window) as a debugging console. Basically, when it is double clicked, it launches a command line window and then the program creates all the GUI windows. Then you'd have two Windows: the main GUI and a debugging console.
I'm trying to port this pogram to OS X. Because OS X (and all Unix OSs for that matter) doesn't automatically launch a command line window when you run a command line application. So, I obviously need another way to port this application.
My initial thought was simply to import the source code into a XCode project, redirect standard input and output and then port the GUI. The GUI and console would run side by side just like in Windows. I don't think this is the most optimal solution since that would mean I'd essentially have to write a terminal emulator.
My other thought would be to port the application as a command line application which creates its GUI just like in Windows. The application would then have to be run from Terminal.app which could handle all the I/O. Unfortunately, I don't think you can use the Cocoa framework without using a NSApplication loop.
Any ideas how I could approach this?
You can of course create a run loop from a terminal-launched app. But that generally isn't what you want to do.
It sounds like on Windows, the CLI is just being used as a shortcut to creating a real debugging console window. So the simplest answer is to create a debugging console window. It's pretty easy to create a window which contains just a multi-line text or list view. (If you want something fancier, consider borrowing code from iTerm2 or other open source projects instead of trying to build a complete terminal.) If your debug information is being printed with some fancy macros, just change the macros to log to your list view.
If you're directly doing something like fprintf or syslog to do your logging, it might be simpler to create a wrapper app that launches the main app, and the wrapper creates the debugging console window and displays the main app's stdout and/or stderr. (This might be as simple as using popen.)

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

multi tabbed SSH client using screen

Is there an SSH client that can present a client side GUI interface to the screen* program?
I'm thinking of an SSH program that would hook in with screen's session handling and map client side actions (clicking on a tab, ctrl-tab, scrolling, possibly even allowing several tabs to be seen at the same time) to whatever it takes to make screen at the other end do it's thing.
* The screen program that allow multiple virtual consoles under a single terminal session, for example you can run several apps under a single SSH connection and switch between them as well as other cool things.
An interesting idea, and quite possible (vim7's tabs show as clicky GUI tabs in gnome-terminal), but I don't see the benefit of doing this..
Using the follow ~/.screenrc shows "graphical" tabs:
startup_message off
vbell off
hardstatus alwayslastline
hardstatus string '%{gk}[ %{G}%H %{g}][%= %{wk}%?%-Lw%?%{=b kR}(%{W}%n*%f %t%?(%u)%?%{=b kR})%{= kw}%?%+Lw%?%?%= %{g}]%{=y C}[%d/%m %c]%{W}'
..which look like the following (after renaming the tabs using ctrl+a,a:
x http://img216.imageshack.us/img216/9401/picture4myi.png
You can scroll around in a screen session using "copy mode", by doing ctrl+a,[ and using the cursor keys (press Esc or ctrl+c to exit it)
You can also attach to the same screen session multiple times using the screen -x flag (rather than -r), so you could use any tabbed terminal emulator, and open one tab for each screen-window.
If you really did want to start implementing this - one option would be to look into modifying gnome-terminal, to copy the behaviour with vim's tabs for screen. Or, write your own screen client - you don't need to do anything as fragile sounding as scraping the terminal - there's a FIFO file in (usually) /tmp/uscreens/S-$USER/$PID.sessionname which I think is how screen communicates, and remember screen is open-source!
Interesting idea. I use screen everyday both on my local machine and for SSH sessions. I think your biggest problem is that I suspect most screen users are commandline junkies like me who just won't see the benefit of making a gui for tabs. In fact, I have all my terminals in one gnome-terminal window under different tabs, and having screen's text-based tabs is a nice way not to confuse the two.
I suspect it could be done, but you'd be writing a specialised terminal emulator which analyses screen's output (custom .screenrc) and retrofits the gui.
A lot of work for minimal gain.
ctrl+a shift+'
.. gui front-end to screen? what are you talking about??
also, because my rep is so low, and i cant comment, id like to LOL # geoffc for his comment in the question
I've never seen one, but the following may help you. Add to your .screenrc
To show a row of "tabs" on the bottom
caption always "%{.bW}%-Lw%{.rW}%n %t%{-}%+Lw %=%{..G} %{..Y} %m/%d"
To show the current program as the screen name [assuming you're using bash and your prompt ends with "$ " by default; others shells are the exact same idea]
shelltitle "$ |sh"