mgutt Posted June 23, 2021 Share Posted June 23, 2021 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: 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 1 Quote Link to comment
mgutt Posted July 23, 2022 Author Share Posted July 23, 2022 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. Quote Link to comment
Squid Posted July 23, 2022 Share Posted July 23, 2022 While I don't still do this, Might also be helpful in conjunction with this. 1 Quote Link to comment
Recommended Posts
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.