Sunday, April 25, 2010

bash: send a command to a screen session

"Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells." It is a very useful tool, that I often use, as it allows me to run "multiple terminals in one" even when I log out from the system.

So how to send command to screen session from other terminal. In other words how to remote-control a screen session from within a script. This post is useful for this.

In particular, I wanted to execute matlab scripts in screen windows. To do this I used the following:
1. Create screen sessionscreen -S matlab2. Create few windows in the screen session using Ctr-a c
3. Send matlab command to each as followsfor ((i=0;i<$NO_WINDOWS;i+=1)); do
echo $i
sleep 2
screen -S matlab -p $i -X exec matlab -nodesktop -r "bashCalculations('part',$i+1)"
done
where bashCalculations is my matlab script bashCalculations.m that takes part argument. So,, each screen window executes different part of my script.