Created
May 26, 2020 06:31
-
-
Save qlixed/2ec7b2c131d8d22abfd5a26dd6508c1d to your computer and use it in GitHub Desktop.
Keep a X / GUI program running in background when it restarts. Check the comment for more info!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Keep a program running in background when it restart. | |
# I make a generalization of the script ot use with any binary, but | |
# initially I use it for Discord as is show here. | |
# Discord have a know bug of the chromium/electron platform that | |
# make it go down sometimes. | |
# If you're using discord to coordinate with your friends a game you | |
# will be doomed with this issue!. | |
# This script will keep discord running and minimize it when is | |
# restarted to avoid any interuption of your game | |
# Requirementes: | |
# - wmctrl | |
# - xdotool | |
# On Fedora: sudo dnf install wmctrl xdotool | |
# On Debian: sudo apt-get install wmctrl xdotool | |
# To efectively close discord you will need to hit CtrL+C on the | |
# terminal where you run this script, closing normally discord will | |
# fire up the protection method of this script and will reopen Discord. | |
# We keep a efimeral log on /tmp/ for this. | |
# | |
# Code is not perfect, but it works. Considering that is just a quick | |
# 20-minutes script done so I can keep Dwarfing with Deep Rock Galactic | |
# is not so bad. | |
# You can change here Discord with any other X app. | |
BIN_NAME=Discord | |
# Name on the bin in the wmctrl -l command | |
WIN_NAME=${BIN_NAME} | |
# Binary name in the ps output | |
PS_BIN_NAME=${BIN_NAME} | |
# You can set this directly or let find the binary as is show here. | |
KEEPINXBG_BIN=$(whereis -b ${BIN_NAME} | cut -d" " -f2) | |
# Time where the script sends the minimize command in seconds. The script | |
# send 1 minimize command to the windows per second. | |
MIN_TRY_TIME=10 | |
count=0 | |
catch_it() { | |
echo "Restarted ${count} times, Closing up!" | |
exit 0 | |
} | |
keep_in_the_back(){ | |
echo 'Trying to send to back' | |
mini_tries=0 | |
# Send the minimize command multiple times as Discord opens 2 or 3 windows | |
# during the startup. | |
while [[ $mini_tries -lt 10 ]]; do | |
# xdotool send the minimize message to the window ID we want. | |
# wmctrl give us the window ID. | |
# (2) Grep just the windows that we need, in this case "Discord Updater" is a | |
# non-visible window that start first, that could give error if we try to minimize. | |
xdotool windowminimize $(wmctrl -l | grep ${WIN_NAME} | cut -d" " -f1) &> /dev/null | |
let mini_tries++ | |
sleep 1 | |
done | |
echo 'Hope is in the back now!' | |
} | |
keep_running() { | |
echo "Keep running" | |
while [[ 1 ]]; do | |
# Checking if is running | |
sleep 2 # No need, but you can uncomment if you want to let it process | |
# the core on the system if is needed. | |
pgrep Discord &> /dev/null | |
if [[ $? != 0 ]];then | |
let count++ | |
echo "Restarting ${BIN_NAME}" | |
keep_in_the_back & | |
$KEEPINXBG_BIN &> /tmp/discord-keep.log | |
fi | |
done | |
} | |
trap 'catch_it' SIGINT | |
echo "*****************************" | |
echo "Remember that you need to use CTRL+C here to close ${BIN_NAME}." | |
echo "*****************************" | |
pgrep ${PS_BIN_NAME} &> /dev/null | |
if [[ $? == 0 ]];then | |
echo " ${BIN_NAME} is started, Please close it and run this script." | |
exit 1 | |
fi | |
$KEEPINXBG_BIN &> /tmp/discord-keep.log | |
keep_running |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment