Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run commands in GNU Screen windows from .screenrc

Tags:

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

like image 264
maxenglander Avatar asked Sep 28 '11 14:09

maxenglander


2 Answers

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.

like image 199
xeorem Avatar answered Sep 18 '22 08:09

xeorem


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"
like image 41
Keith Thompson Avatar answered Sep 19 '22 08:09

Keith Thompson