I'm writing a script to automate work environment preparation. I need to open 4 terminal windows, arrange them and execute commands in each of them.
It works, but sometimes I get nasty fails - xdotool type
randomly repeats some characters:
rvm use ruby-1.99999999999999999999999999999999.3-p194@ro && rails c
~/my_src/ruby_apps/ro > rvm use ruby-1.99999999999999999999999999999999.3-p194@ro && rails c
ruby-1.99999999999999999999999999999999.3-p194 is not installed.
To install do: 'rvm install ruby-1.99999999999999999999999999999999.3-p194'
~/my_src/ruby_apps/ro >
or in the other window:
tail -fn 100 looooooog/thin.0.log
~/my_src/ruby_apps/ro > tail -fn 100 looooooog/thin.0.log
tail: could not open «looooooog/thin.0.log» for reading: No such file or directory
tail: no more files
~/my_src/ruby_apps/ro >
I guess it depends on CPU load, cause I have really big .bashrc processed by ATOM and its load is high during script processing.
I use wait
and sleep
and special order of open_lxterminal_execute_hold()
function invocations in the script toexecute simple simple commands first. That minimizes errors, but doesn't prevent them at all.
What would you suggest to get stable result regardless of CPU load(whatever)? It would be great to get rid of sleep
s as well.
#!/bin/bash
#
# prepares work environment for rails project
# Opens lxterminal with title if windows with such title
# doesn't exist, executes command and stays open.
# Otherwise does nothing.
#
function open_lxterminal_execute_hold(){
local winid=`xwininfo -name $title 2>/dev/null |grep 'Window id:' |cut -d" " -f4`
if [ -n "$winid" ]; then
echo "Window for title '$title' exists with '$winid'"
else
lxterminal -t $title
sleep 1
wmctrl -i -a "$winid" # bring the window to front, activate
wait
xdotool type "$command"
wait
xdotool key Return # run the command
wait
fi
}
pkill devilspie
cd ~/my_src/ruby_apps/ro # TODO param
title='rails-commandline'; command='ls'; open_lxterminal_execute_hold
title='rails-development.log'; command='tail -fn 100 log/development.log'; open_lxterminal_execute_hold
title='rails-user_case'; command='tail -fn 100 log/thin.0.log'; open_lxterminal_execute_hold
sleep 5
title='rails-console'; command='rvm use ruby-1.9.3-p194@ro && rails c'; open_lxterminal_execute_hold
/usr/bin/devilspie -a 2>/dev/null & # arrange windows
UPDATE How to prevent xdotool repeating charatcers?