Last active
April 17, 2023 16:25
-
-
Save hydrian/bb6aa7a31443d0f28b702e84d83f9962 to your computer and use it in GitHub Desktop.
Script to prevent sleep while docker specific containers is running
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
#!/usr/bin/env bash | |
############## | |
### Config ### | |
############## | |
DOCKER_CONTAINER_NAMES=( | |
"CONTAINER NAME" | |
"CONTAINER NAME2" | |
) | |
################## | |
### Processing ### | |
################## | |
APP_NAME="$(basename "$0")" | |
PM_ACTION="${1}" | |
LOGGER_BIN="$(which logger)" | |
SYSLOG_FACILITY="daemon" | |
LOGGER_BASE="${LOGGER_BIN} -t ${APP_NAME} " | |
DOCKER_BIN="$(which docker)" | |
### Validate if docker is installed. Fail Gracefully | |
if [ $? -ne 0 ] ; then | |
"${LOGGER_BASE}" -p ${SYSLOG_FACILITY}.error -- "Docker executable is not installed. Assumed not running" | |
exit 0 | |
fi | |
case "${PM_ACTION}" in | |
"suspend"|"hibernate") | |
for CONTAINER_NAME in "${DOCKER_CONTAINER_NAMES[@]}" ; do | |
"$DOCKER_BIN" ps --filter "status=running" --no-trunc |grep -q "${CONTAINER_NAME}" | |
DOCKER_RET=$? | |
if [ ${DOCKER_RET} -eq 0 ] ; then | |
"${LOGGER_BASE}" -p ${SYSLOG_FACILITY}.notice -- "docker container ${CONTAINER_NAME} is blocking the system from going to into ${PM_ACTION}" | |
exit 1 | |
else | |
"${LOGGER_BASE}" -p ${SYSLOG_FACILITY}.debug -- "docker container ${CONTAINER_NAME} is not blocking the system from going to into ${PM_ACTION}" | |
fi | |
done | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment