Skip to content

Instantly share code, notes, and snippets.

@Jcpetrucci
Last active October 26, 2018 18:56
Show Gist options
  • Save Jcpetrucci/c39e706640c5662ab06e4448d9e75dd2 to your computer and use it in GitHub Desktop.
Save Jcpetrucci/c39e706640c5662ab06e4448d9e75dd2 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ "${1,,}" == "install" ]]; then
# Call this script with argument 'install' to generate a systemd unit file.
cat <<-EOF | sudo dd of=/etc/systemd/system/vnc-alive-check.service
[Unit]
Description=VNC-ALIVE-CHECK: Make sure VNC is connected and restart it if needed.
After=network.target
[Service]
ExecStart=${PWD}/$0
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable vnc-alive-check.service
sudo systemctl start vnc-alive-check.service
exit 0
fi
function func_restartRemmina {
killall remmina
sleep 2
#su --command='DISPLAY=:0 XAUTHORITY=/var/run/lightdm/root/:0 remmina --connect=/home/user/.remmina/1513445416363.remmina &' user
su --command='DISPLAY=:0 remmina --connect=/home/user/.remmina/1513445416363.remmina &' user
}
#### MAIN ####
function main {
var_rchar=0 # initialize counter for later comparison
while :; do
sleep 10
# no pid at all
pidof remmina &>/dev/null || { echo Restarting due to no PID; func_restartRemmina; }
# pid but no socket
lsof -i -a -p $(pidof remmina) -n -P 2>/dev/null | grep -q ESTABLISHED || { echo Restarting due to no socket; func_restartRemmina; }
# socket but no data
var_lastRchar="$var_rchar"
var_rchar="$(sed -n -r 's/rchar: ([0-9]+)$/\1/p' /proc/$(pidof remmina)/io)"
(( $var_rchar > $var_lastRchar )) || { echo Restarting due to not enough rchar: $var_rchar current / $var_lastRchar last; func_restartRemmina; }
done
}
[[ -z "$1" ]] && main # main loop only runs when the script has no arguments. this prevents main from running when script is actively run to perform a specific function.
@Jcpetrucci
Copy link
Author

For remmina VLC sessions that stay open (meaning process is running and socket is open), but no data comes back from the server, use an aggressive TCP timeout:

sysctl -w net.ipv4.tcp_keepalive_time=30
sysctl -w net.ipv4.tcp_keepalive_intvl=3
sysctl -w net.ipv4.tcp_keepalive_probes=5
sysctl -w net.ipv4.tcp_retries2=5 

May be able to change socket options to use "TCP_USER_TIMEOUT" to avoid setting system-wide configuration.
See https://patchwork.ozlabs.org/patch/62889/

"TCP_USER_TIMEOUT is a TCP level socket option that takes an unsigned int,
when > 0, to specify the maximum amount of time in ms that transmitted
data may remain unacknowledged before TCP will forcefully close the
corresponding connection and return ETIMEDOUT to the application. If
0 is given, TCP will continue to use the system default."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment