-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckConnectionsAndReboot.sh
39 lines (34 loc) · 1.52 KB
/
CheckConnectionsAndReboot.sh
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
#!/bin/bash
#
# Linux bash script that will reboot the server after checking if there is any current SSH-connections to the server using native tools (i.e. ss).
# Purpose: Reboot server if there is no data being transfered over the connections.
#
# https://github.com/godstoge/LinuxScripts/blob/main/CheckConnectionsAndReboot.sh
# https://raw.githubusercontent.com/godstoge/LinuxScripts/main/CheckConnectionsAndReboot.sh
echo "------------------------------------------------------------------------------"
date
uptime
echo " "
if ss -t '( dport = :22 or sport = :22 )'
then
echo "Found SSH-connections in ss";
# Get LastRcvd (in milliseconds) to the connections
connections=$(ss -i '( sport = :ssh or sport = :ssh )' | awk 'match($0, /last/) {print substr($0, RSTART)}' | awk -F ":" '{print $3" "$4}' | awk -F " " '{print $1}')
# Iterate through the LastRcvd values to determine if any of them have been idle for more than 10 seconds
# The assumption being that we have clients that are just idling with a keepalive, but most importantly: Are not transfering data to the SFTP-server.
clearedForReboot=1 #yes
for connection in $connections
do
if [ $connection -lt 10000 ];
then echo "Connection under 10 seconds: $connection";
clearedForReboot=0 #Remove Cleared For Reboot-flag
else
echo "Connection over 10 seconds: $connection";
fi
done
echo "Conclusion - server is clear for reboot (true/False): $clearedForReboot"
if [ $clearedForReboot -eq 1 ]
then
/sbin/shutdown -r now
fi
fi