Automatically start containers by dir or stop inactive containers (e.g. Minecraft)


Recommended Posts

My son should be able to start/stop a Minecraft container without getting access to the Unraid GUI. And to save some electricity costs, the container should even automatically stop if it's not used anymore.

 

At first I added through the SMB (extra) settings a new SMB Share as follows (replace "max,moritz" against your usernames):

[Minecraft-Server]
	path = /mnt/user/appdata/Minecraft-Server
	comment =
	browseable = yes
	# Private
	writeable = no
	read list = 
	write list = max,moritz
	valid users =  max,moritz
	case sensitive = auto
	preserve case = yes
	short preserve case = yes

 

This gives my son access to the Minecraft appdata subdirectory, where he is able to create a dir called "stop" to stop the container or he deletes it, which automatically starts the container:

image.png.1a4fc3d1a8019df54231355dc976142e.png

 

Donate? 🤗

 

This works because of the following script, which I added through the User Scripts plugin (set custom cron to */1 * * * * to execute it every minute):

 

#!/bin/bash

# #####################################
# Script:      Container Autostop v0.2
# Description: Automatically stops and starts a container if a "stop" dir is present or
#              if the last log entry is too old
# Author:      Marc Gutt
# ######### Settings ##################

# Finds all containers with this keyword in their name (Regex is allowed)
container_name_contains="Minecraft"

# Containers log path
container_log_path="/logs/latest.log"

# Stops a container if <container_host_path>/<container_log_path> is older than X minutes
container_inactivity_minutes=120

# #####################################
# 
# ######### Script ####################

# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi; trap 'rmdir "/tmp/${0///}"' EXIT;

# loop through all containers with "keyword" in their name
for container_name in $(docker ps --all --format "{{.Names}}" | grep -P "$container_name_contains"); do

  echo "Process $container_name"

  # obtain containers host paths
  container_host_path=$(docker container inspect -f '{{$id := .Id}}{{range .Mounts}}{{if .Source}}{{.Source}}{{end}}{{end}}' "$container_name")
  stop_path="$container_host_path/stop"
  log_path="$container_host_path$container_log_path"

  # check if host path has been found
  if [[ -f "$container_host_path" ]]; then
    echo "Error: $container_host_path of $container_name does not exist!?"
    exit 1
  fi

  # container is running
  if [[ $( docker container inspect -f '{{.State.Running}}' "$container_name" ) == "true" ]]; then

    echo "Container is running"

    # "stop" dir is present
    if [[ -d $stop_path ]]; then
      echo "Stop container as $stop_path is present"
      docker stop "$container_name"

    # inactivity timeout has been reached
    elif [[ $(find "$log_path" -mmin +$container_inactivity_minutes 2>/dev/null) ]]; then
      echo "Stop container $container_name as inactivity timeout has been reached"
      docker stop "$container_name"
      # create stop dir
      mkdir "$stop_path"

    # nothing to do
    else
      echo "$log_path is not older than $container_inactivity_minutes minutes"
    fi

  # start container if "stop" dir is not present
  elif [[ ! -d $stop_path ]]; then
      echo "Start container $container_name as $stop_path is not present"
      docker start "$container_name"

  # nothing to do
  else
    echo "Container is not running and as $stop_path is present, it won't be started."
  fi

done

 

 

  • Thanks 1
Link to comment
  • mgutt changed the title to Automatically start containers by dir or stop inactive containers (e.g. Minecraft)

New version 0.2 released.

 

Now it even automatically stops the container if /mnt/user/appdata/minecraft/vanilla/logs/latest.log is older than 120 minutes (this log is filled with the commands which are displayed in the Minecraft console).

 

Log path and container names are variable now, too. This means the script can be used for other containers as well.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.