0

I wrote a shell script that runs a python program. Then I want to load a file into the program using xdotool. Right now this is my code:

#!/bin/bash
cd ~/Folder
python program.py &
sleep 10
WID=$(xdotool search --onlyvisible program)
....

I really don't like my solution of just waiting 10 seconds so that the program is loaded. Is there a better way?

MilloMille
  • 35
  • 1
  • 8
  • Have your Python program start the second part of the script? – kindall Apr 09 '17 at 22:00
  • I didn't write the python program. Its a open source software – MilloMille Apr 09 '17 at 22:21
  • `xdotool search` has a `--sync` option that will wait for the window to become visible. If you run it with that option (i.e. `xdotool search --sync --onlyvisible program`) then your script does not need to `sleep` at all. – ottomeister Apr 10 '17 at 02:02
  • @ottomeister Thank You!! That was the solution to my Problem. Will it still work 100% of the time, even without sleep? I mean sometimes windows are visible but not ready? – MilloMille Apr 10 '17 at 08:37
  • That depends on how the program is written. Typically, by the time a window becomes visible the program has already arranged to accept and handle events that are sent into the window, so you should be safe with no sleep. If you want to be extra cautious you could sleep for a second or two after `xdotool search` returns. That's still better than the original 10 second sleep. – ottomeister Apr 10 '17 at 09:23
  • @ottomeister thanks a lot for your help. i tried it a few times and it seems to work. out of coution i included a one second break anyway. Thanks again!!!! – MilloMille Apr 10 '17 at 14:19

1 Answers1

0

This really depends. If program.py is supposed to finish then go on to xdotool then you might want to use && instead of &. A single & means you want the command to execute then move on to the next command as soon as possible without waiting for it to finish. A dobule && means you want to wait for the execution to be done, then only continue if you have a zero error exit. You could also just remove the & or use ; if you want to run the next command regardless of program.py's success. Here's what I'm getting at:

#!/bin/bash
cd ~/Folder
python program.py &&
WID=$(xdotool search --onlyvisible program)
...

If program.py is supposed continue run while you run the xdotool command, but you need program.py to come to some ready state before you continue, then you're right in using &, but you need to monitor the program.py somehow, and get a signal from it that it is ok to continue. An ancient way of doing this is to simply let program.py create/touch/edit a file, and when that file is detected you continue. A more advanced way would be to use network sockets or similar, but I'd advice against it if you really don't need to do anything fancy. Anyway, if you make program.py create a signal file, say /tmp/.program_signalfile, when things are ready all you have to do is:

#!/bin/bash
cd ~/Folder
python program.py &
until [ -f /tmp/.program_signal_file ]
do
    sleep 1
done
WID=$(xdotool search --onlyvisible program)
rm /tmp/.program_signal_file
...

In this last solution program.py and xdotool are both running at the same time. If that's what you were looking for.

André C. Andersen
  • 8,955
  • 3
  • 53
  • 79
  • Thanks what you said was really useful. I need the program to run while i run the xdotool command. But how do i make the program create a file? It is a open source software that i didn't write. – MilloMille Apr 09 '17 at 22:22
  • In the source code there is a line called app.MainLoop() Do I create the file right before that? But how do i create a file? Sorry I'm still pretty new to this – MilloMille Apr 09 '17 at 22:29