Assuming the change in resolution is both causing the error and the trigger to run your script, periodicalloy checking the resolution and run your script if it changes should do the job:
#!/usr/bin/env python3
import subprocess
import time
command = "</path/to/script_to_run>"
def get_res():
# get resolution
xr = subprocess.check_output(["xrandr"]).decode("utf-8").split()
pos = xr.index("current")
return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]
res1 = get_res()
while True:
time.sleep(5)
res2 = get_res()
if res2 != res1:
subprocess.Popen(["/bin/bash", "-c", command])
res1 = res2
It checks the current resolution every five seconds and runs the script, as set in the head section, if there is a resolution change.
How to use
Copy the script into an empty file, save it as check_resolution.py
In the head section, set the path to your script to restart synergy
Test-run it by running it in a terminal window with the command:
python3 /path/to/check_resolution.py
if all works fine, add it to your startup applications.
Note
The command as a startup application probably needs a small break to make sure the desktop is fully loaded when the script starts. The command to add to Startup Applications would then be:
/bin/bash -c "sleep 15&&python3 /path/to/check_resolution.py
Add to Startup Applications: Dash > Startup Applications > Add, add the command.
Edit
Just to be complete:
If for some reason, you'd prefer a bash version of the script:
#!/bin/bash
function get_res {
echo $(xrandr -q | awk -F'current' -F',' 'NR==1 {gsub("( |current)","");print $2}')
}
res1=$(get_res)
while true; do
sleep 5
res2=$(get_res)
if [ "$res1" != "$res2" ]; then
<command_to_run>
fi
res1=$res2
done
The use is pretty much the same except the language extension:
check_resolution.sh
and the command to run it:
/bin/bash /path/to/check_resolution.sh
and replace <command_to_run> by the command to restart synergy or the path to your script.