Start/Stop container by non-root user


Recommended Posts

As one of my non-root users should be able to start / stop a Minecraft container without getting access to the Unraid WebGUI, I came up with this script.

 

Manual

  1. Install User Scripts Plugin
  2. Add script and set container name and stop_dir path
  3. define custom cron */1 * * * * image.png.93ccc70d22431e5abd58994328755622.png
  4. create dir "stop" in the target path and container is stopped
  5. remove dir "stop" from the target path and container is started

 

Donate? 🤗

 

Script

#!/bin/bash
# #####################################
# Script:      Container Start/Stop by Dir
# Description: Starts or stops a container by checking the existance of a local dir
# Author:      Marc Gutt
# ######### Settings ##################

# name of the container that should be started / stopped
container_name="Minecraft-Server"

# if this directory exists, the docker container will be stopped (else started)
stop_dir="/mnt/cache/appdata/Minecraft-Server/stop"

# #####################################
# 
# ######### Script ####################
# make script race condition safe
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi; trap 'rmdir "/tmp/${0///}"' EXIT;
# check if container exists
if docker container inspect ${container_name} > /dev/null 2>&1; then
    # stop container
    if [[ -n $(find "$(dirname "$stop_dir")" -maxdepth 1 -iname "$(basename "$stop_dir")") ]]; then
        if [ "$( docker container inspect -f '{{.State.Running}}' ${container_name} )" == "true" ]; then
            echo "Stop container..."
            docker stop ${container_name}
        fi
    # start container
    elif [ "$( docker container inspect -f '{{.State.Running}}' ${container_name} )" == "false" ]; then
        echo "Start container..."
        docker start ${container_name}
    fi
fi

 

 

Link to comment

Example

 

My Minecraft Server uses the path "/mnt/cache/appdata/Minecraft-Server". I granted SMB access to this folder by adding the following code to /config/smb-extra.conf

[Minecraft-Server]
	path = /mnt/cache/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

 

After that I executed this to apply the changes:

samba restart

 

Now the users are able to create the dir "stop" inside this path and the container gets stopped:

1451941615_2021-06-2314_59_44.png.6f31306a7aaae3f411cc5e14d68cf3d1.png

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.