Created
January 9, 2016 06:09
-
-
Save JLarky/8a4033688d516ac9a5d6 to your computer and use it in GitHub Desktop.
Bash script to pause docker container after INACTIVE_SEC of inactivity in project files
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 | |
cd -- "${0%/*}" | |
CHECK_SEC=60 | |
INACTIVE_SEC=600 | |
DIRECTORY="sameroom" | |
TIMEOUT_TMP_FILE="./timeout" | |
DOCKER_NAME="sameroom-dev" | |
PID=$$ | |
timeout() { | |
while true | |
do | |
sleep 1 | |
# make sure that parent is running | |
kill -s 0 $PID 2>/dev/null || exit | |
touch $TIMEOUT_TMP_FILE | |
sleep $CHECK_SEC | |
done | |
} | |
timeout & | |
TIMEOUT=$! | |
LASTACTIVE=0; | |
OLDSTATE=""; | |
fswatch $DIRECTORY $TIMEOUT_TMP_FILE | while read line | |
do | |
NOW=`date +%s` | |
if ! echo $line | grep -q timeout | |
then | |
LASTACTIVE=$NOW | |
fi | |
SINCELASTACTIVE=$(($NOW-$LASTACTIVE)) | |
if [[ $SINCELASTACTIVE -gt $INACTIVE_SEC ]] | |
then | |
STATE="pause" | |
else | |
STATE="unpause" | |
fi | |
if [[ "$OLDSTATE" != "$STATE" ]] | |
then | |
date | |
echo DO $STATE | |
docker $STATE $DOCKER_NAME | |
fi | |
# echo line $line $NOW $LASTACTIVE $SINCELASTACTIVE $STATE $OLDSTATE | |
OLDSTATE=$STATE | |
done | |
kill $TIMEOUT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment