Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Run script when specific docker starts

Featured Replies

Hi, Is there a way that I can trigger a script to run whenever a specific docker starts? The options in the User Scripts plugin doesn't have this usecase. Thanks

Solved by JonathanM

  • Solution

Not that I can think of, but you can certainly start a container from a script. Disable the auto start for that container on the docker page and script the startup separately.

schedule at array start

sleep however long you want to wait

docker start containername

run the rest of your commands.

  • Author
19 hours ago, JonathanM said:

Not that I can think of, but you can certainly start a container from a script. Disable the auto start for that container on the docker page and script the startup separately.

schedule at array start

sleep however long you want to wait

docker start containername

run the rest of your commands.

Thanks, that is basically what I ended up doing. Running this on a cron every few minutes to check if the docker is running and if not it starts it up and executes the commands I need. Sharing the script in case anyone else ever needs this.

 

#!/bin/bash

# Replace CONTAINER_NAME with the actual name of your running Docker container
CONTAINER_NAME="CHANGEME"

# Specify the log file path
LOG_FILE="docker_exec_log.txt"

# Check if the container is already running
if docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; then
    echo "Container $CONTAINER_NAME is already running."
    exit 0
fi

# Start the container if it's not running
docker start $CONTAINER_NAME

# Wait for the container to start
echo "Waiting for container $CONTAINER_NAME to start..."
while ! docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; do
    sleep 1
done

echo "Container $CONTAINER_NAME is now running."

# Run commands inside the Docker container and log errors/output
docker exec $CONTAINER_NAME bash -c '
 REPLACE_THIS_WITH_THE_COMMANDS_YOU_WANT_TO_RUN
' 2>&1 > $LOG_FILE

# Display the log file contents
cat $LOG_FILE

 

Edited by pkoasne
added script

  • 2 months later...
On 8/12/2023 at 8:19 PM, pkoasne said:

Thanks, that is basically what I ended up doing. Running this on a cron every few minutes to check if the docker is running and if not it starts it up and executes the commands I need. Sharing the script in case anyone else ever needs this.

 

#!/bin/bash

# Replace CONTAINER_NAME with the actual name of your running Docker container
CONTAINER_NAME="CHANGEME"

# Specify the log file path
LOG_FILE="docker_exec_log.txt"

# Check if the container is already running
if docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; then
    echo "Container $CONTAINER_NAME is already running."
    exit 0
fi

# Start the container if it's not running
docker start $CONTAINER_NAME

# Wait for the container to start
echo "Waiting for container $CONTAINER_NAME to start..."
while ! docker inspect -f '{{.State.Running}}' $CONTAINER_NAME > /dev/null 2>&1; do
    sleep 1
done

echo "Container $CONTAINER_NAME is now running."

# Run commands inside the Docker container and log errors/output
docker exec $CONTAINER_NAME bash -c '
 REPLACE_THIS_WITH_THE_COMMANDS_YOU_WANT_TO_RUN
' 2>&1 > $LOG_FILE

# Display the log file contents
cat $LOG_FILE

 

You are awesome for sharing the script! I greatly appreciate it, thank you

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...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.