Run commands in GNU Screen windows from .screenrc - gnu-screen

Is there a way to send a sequence of commands to GNU Screen windows from my .screenrc? It seems like this should be easy to do:
.screenrc:
startup_message off
screen -t "RAILS SERVER"
<send command to last created window> <my alias to cd Rails project>
<send command to last created window> rails s
screen -t "RAILS CONSOLE"
<send command to last created window> <my alias to cd to Rails project>
rails c
I've gone over the Screen man-page several times, but can't find anything that will <send command to last created window>.
Thanks,
Max

Keith's answer gets the job done but it ties the window to that process so that as soon as the application is done executing, the window closes.
Here's what I wound up doing that worked perfectly:
screen -t "RAILS SERVER"
stuff "cd $my_rails_directory; rails server^M"
screen -t "RAILS CONSOLE"
stuff "cd $my_rails_directory; rails console^M"
The important part to note here is the ^M character. This isn't actually a ^ followed by an M. This is a raw newline character. In almost any CLI program (vi, emacs, shell), you can press CTRL-V and then press ENTER to generate this character.
How does this work? The stuff command types the given string directly into the console. The newline literal at the end actually sends the command off the way you normally would if you typed it yourself. Hope that helps! I've found this approach to be far more stable and reliable than others.

It's not a separate command; you just specify the command to run on the line that creates the window.
For example (untested):
screen -t "RAILS SERVER" sh -c "cd ... ; rails s"

Related

How to start a Rails server with an .sh script in a directory of choice

I've been developing with rails for a year now, and although rails is pretty well automated, I'd like to take it one step further.
Every time i start working on a project, i open the terminal, cd to a project folder, than new tab in the terminal, then start the server with "rails s" then back to the first tab to run "sublime ." so that i load the folder in my text editor, Sublime Text.
What i would like is to create a .sh script that would do all that for me, but so far i haven't been able to make it start the server.
So, how to start rails server with a .sh script in a directory of choice?
#Manolo gave me an idea, so I've come up with this:
I modified my .bashrc as the following answer illustrates:
https://superuser.com/a/198022
Basically i added
eval "$BASH_POST_RC"
At the end of my .bashrc so i could run arbitrary commands after it was executed.
Next, i made a following script:
#launch_project.sh
#/bin/bash
cd <PROJECT DIR GOES HERE>;
firefox -P "development";
sublime . &;
gnome-terminal \
--tab --title="Server" -e 'bash -c "export BASH_POST_RC=\"rails s\";exec bash"' \
--tab -e 'bash -c "export BASH_POST_RC=\"git s\"; exec bash"';
That launches my development profile on firefox, my editor with a project root, and terminal window with two tabs - one that runs a WEBrick server, and another one which runs git status for me.
I made a desktop shortcut to the script so i have my own custom IDE of sorts :)
Try this:
#!/bin/sh
cd your_project_folder
nohup rails s > /tmp/rails_server.log 2>&1 &
sublime .
you can see the ouput of your rails server at the /tmp/rails_server.log file

bash script for screen -r

I want to make a bash script that echo's something into one of the screens that I have running (screen -r is how I get to it in SSH).
I was wondering how I would make the script execute itself in screen -r?
I basically just want the script to say something on a minecraft server through the console and would set up a cronjob to say it every x minutes.
Cheers,
You can use the -X option of screen to send commands to a running screen session.
Also the -p option is useful in this case, as you can use it to preselect a window
As an example you can run a script in a running screen session on windows 0 via:
screen -p 0 -X stuff './fancy_script.sh^M'
Note, that you have to append the return key-code to execute the script.
You can look in /dev/pts. I don't have screen here to test, but you can echo something to an opened terminal with, for example, echo "toto" > /dev/pts/0 (it will be echoed on the first opened terminal).

How to create a screen executing given command?

i'm fairly new in *nix. Is there a way to create a screen, which will immediately execute a given command sequence (with their own arguments)? Two hours of googling yields nothing - perhaps because I can't
clearly state the question.
I hope for something like
screen -dmS new_screen exec "cd /dir && java -version"
I am using screen v4.00.03 and CentOS 5.5 (kernel ver. 2.6.18-194.26.1.el5.028stab079.2)
You create a screen with a name and in detached mode:
screen -S "mylittlescreen" -d -m
Then you send the command to be executed on your screen:
screen -r "mylittlescreen" -X stuff $'ls\n'
The stuff command is to send keystrokes inside the screen. The $ before the string command is to make the shell parse the \n inside the quotes, and the newline is required to execute the command (like when you press enter).
This is working for me on this screen version:
$ screen -v
Screen version 4.00.03jw4 (FAU) 2-May-06
Please see man screen for details about the commands.
The problem is that using the 'exec' screen command does not start a shell. 'cd' is a shell builtin, so you need a shell for it. Also, you need a shell that remains running so that screen does not terminate.
You can use the -X option to screen to send commands to a running screen session, and the 'stuff' command to send keystrokes to the current window. Try this:
screen -dmS new_screen sh
screen -S new_screen -X stuff "cd /dir
"
screen -S new_screen -X stuff "java -version
"
Yes, you need to put the quotes on the next line in order for the commands to be executed.
screen -dmS screen_name bash -c 'sleep 100'
This will create new screen named screen_name. And inside the screen it will sleep for 100 seconds.
Note that if you type some command in place of sleep 100 which terminates immediately upon execution, the screen will terminate as well. So you wont be able to see the screen you just created
I wanted to launch remote screens from within a bash script with some variables defined inside the bash script and available inside screen. So what worked for me was
#!/bin/bash
SOMEVAR1="test2"
# quit existing if there is one running already, be careful
screen -D -RR test1 -X quit || true
screen -dmS test1
screen -r test1 -p 0 -X stuff $"echo ${SOMEVAR1} ^M"
Where the return character, ^M, you need to enter using vim as
i CTRL-V ENTER ESCAPE
Another approach
First line cd to the your directory.
Second line start a new screen session named new_screen with bash.
Third line executing java -version
cd /dir
screen -dmS new_screen bash
screen -S new_screen -p 0 -X exec java -version
I think that you can use this
function exec_in_screen() {
name=$1
command=$2
screen -dmS $name sh; screen -S $name -X stuff "$command\n";
}
Then...
exec_in_screen "test" "ls"
Yes, what you want is the "stuff" command
for example
screen -dmS new_screen -X stuff "cd /dir && java -version
"
the second quote is on the next line so that it executes when sent

how do you script gnu screen from within a screen session to open new windows and run commands in them?

From within a screen session, I'd like to run a shell script that opens
a few new screen windows in the same session and start running some
programs in them.
I need a script like this:
screen -t newWindow
[switch to newWindow and execute a command]
screen -t newWindow2
[switch to newWindow2 and execute a command]
I don't know how to accomplish the effect I describe in the brackets.
Any clues? Please note that this is not a script I'll be running to start a screen session. I need this script to be runnable within an existing screen session, in order to add new windows to the session.
Note: you can't launch script working following way from a screen session. And it will open in session no tabs... Its more a related tip than a real answer to the question.
There is an other solution, if you accept to have a screen session by running process...
new session script
#!/bin/sh
echo "nouvelle session screen ${sessionName}"
screen -S ${sessionName} init.sh
echo "screen session: done"
echo "go to ${AnyWhere}"
sleep 1
screenexec ${sessionName} "cd ${AnyWhere}"
init script (here "init.sh")
#!/bin/zsh
zsh -c "sleep 0.2"
screen -d #detach the initialised screen
zsh #let a prompt running
injection script (here screenexec)
#!/bin/sh
# $1 -> nom de screen cible $2 -> commande
echo "injection de «${2}» dans la session «${1}» ..."
screen -x "$1" -X stuff "$2" #inject the command
screen -x "$1" -X eval "stuff \015" #inject \n
echo "Done"
By using this way, you should inject code easily in your screens, interesting if your script act like a deamon...
For those who prefer script in python, I've made a small lib to create sessions, close sessions, inject commands: ScreenUtils.py
It's a small project, which don't handle multiwindows screen sessions.
Forgot to mention I made a real python library out of it long ago: https://github.com/Christophe31/screenutils
Running this script inside screen does what I think you want:
#!/bin/bash
screen vi
screen top

running command on remote machine using ssh

i want to run some command on several machine using ssh. I know it can be done by just using the command "ssh user#hostname command". However, the command i want to run print some string on the console. Is there any way that send all the strings back to the console that i'm on?
You could run the commands in a screen:
screen -S test
ssh user#hostname command1
ssh user#hostname2 command2
You can then detach (Ctrl-D) from the screen, let it run for however long it will run, then re-attach (screen -r test) to the screen and see all of the output. This assumes that you won't have a ton of output from the commands, however. Here's a link to a tutorial on screen.
ssh user#hostname command
Does just that. if 'command' outputs something, it'll show on the terminal you ran ssh from.
Try e.g. ssh user#hostname ls -l
But as others have said, GNU screen is invaluable for this type of work.
You probably want to use Gnu Screen for this. You can start a process in a "virtual" terminal, "detach" the terminal and log out for however long you want... Then you can ssh back in and re-attach the terminal to see the console output.
Also have a look at nohup, for example:
ssh user#domain.com nohup script_that_outputs_strings.py > the_strings.txt
Then if you want to go back and monitor the progress, you could check back and tail the file or scp the output back to your local machine.
Why don't you send you an email back?
Or use a log file, and scp it to your current computer?
otherwise, I don't know!