This repository has been archived by the owner on Jan 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperiodicShutdown.sh
72 lines (54 loc) · 2.3 KB
/
periodicShutdown.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
########################################################################################
# This script is designed to be called with no arguments as a periodic cron job. It #
# will check for instances that have been spun up, and shut them down after a set time #
# period elapses (currently set to 2 weeks). They will be able to be started back up #
# manually if desired. To determine an instance's age, the directory timestamp of the #
# instance root folder is used. When an instance is started up afterwards (or #
# recreated for some reason), the timestamp should be updated and then automatically #
# not be shut down again by this script. #
########################################################################################
###############
# Basic Setup #
###############
# Echo out what we're doing
echo "Checking for any instances to shut down (current time: $(date))."
# Enter the folder of instances
cd ~/CICD_TestInstances || { echo "Test Instances Folder Missing, Aborting."; exit 1; }
# Save current time
NOW=$(date +"%s")
# Calculate time offset. seconds * minutes * days * 2 weeks
(( TIMEOUT = 60*60*24*14 ))
# Loop through each folder and check if it's time to shut it down
for entry in "."/*
do
entry=${entry:2}
echo "$entry"
# See if it's running
if docker ps --format '{{.Names}}' | grep -q "$entry"; then
echo "Instance is running."
# Check the modified time
INSTANCETIME=$(stat -c %Y "$entry") # Checks the modified time of the instance folder
# Compare
(( DAYSOLD = ( ( NOW - INSTANCETIME ) / ( 60*60*24 ) ) ))
echo "Instance is $DAYSOLD days old ($(stat -c %y "$entry"))."
# Check if too old
if (( ( NOW - INSTANCETIME ) > TIMEOUT )); then
echo "Shutting down instance."
# Enter the folder to shut it down
cd "$entry/PollBuddy" || { echo "Failed to cd into instance, aborting."; exit 1; }
docker-compose -p "$entry" down
cd ../../ || { echo "Failed to cd out of instance, aborting."; exit 1; }
echo "Instance has been shut down."
else
echo "Instance does not need to be shut down."
fi
else
echo "Instance not running, ignoring."
fi
echo "---"
done
echo "Instance check complete on $(date)."
echo ""
# Exit
exit 0