-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlost_connection_close_program_JBs.sh
More file actions
executable file
·56 lines (48 loc) · 1.93 KB
/
Copy pathlost_connection_close_program_JBs.sh
File metadata and controls
executable file
·56 lines (48 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
#
# Autor= João Batista Ribeiro
# Bugs, Agradecimentos, Críticas "construtivas"
# me envie um e-mail. Ficarei Grato!
# e-mail: joao42lbatista@gmail.com
#
# Este programa é um software livre; você pode redistribui-lo e/ou
# modifica-lo dentro dos termos da Licença Pública Geral GNU como
# publicada pela Fundação do Software Livre (FSF); na versão 2 da
# Licença, ou (na sua opinião) qualquer versão.
#
# Este programa é distribuído na esperança que possa ser útil,
# mas SEM NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a
# qualquer MERCADO ou APLICAÇÃO EM PARTICULAR.
#
# Veja a Licença Pública Geral GNU para mais detalhes.
# Você deve ter recebido uma cópia da Licença Pública Geral GNU
# junto com este programa, se não, escreva para a Fundação do Software
#
# Livre(FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# Script: If lost connection, close some program
#
# Last update: 16/07/2026
time_sleep="5m" #5m # Time to sleep and test again
website_ping="google.com" # google.com # Website to test ping
program_kill=$1 # Program to close when lost connection
count_ping_send=3 #3 # Count of packets of ping to send
RED='\e[1;31m'
NC='\033[0m' # reset/no color
echo -e "\n # Script to test if has connection, if not, close some program #"
if [ "$program_kill" == '' ]; then
echo -e "\nError: not passed the program to kill! Exiting...\n"
exit 1
fi
while true; do
ping_result=$(ping -c "$count_ping_send" "$website_ping" 2>&1)
grep_failure=$(echo "$ping_result" | grep -E "Temporary failure in name resolution|100% packet loss")
echo -e "\n Ping result: $ping_result\n"
if [ "$grep_failure" != '' ]; then
echo -e " $RED - Website unreachable - killall \"$program_kill\" -$NC\n"
killall "$program_kill" # killall send by default SIGTERM - graceful shutdown
fi
echo -n " - Sleep time - ${time_sleep} - "
date
sleep ${time_sleep}
done