1

I'm trying to create a bash script and .desktop shortcut that toggle (start and stop) the ComfyUI server. This is my current working bash script to start the ComfyUI server:

#!/bin/bash

# activate the venv
source ./AI/ComfyUI/venv/bin/activate

# start ComfyUI
python3 ./AI/ComfyUI/main.py

Taking some clues from this question, How do I check with a Bash script if an application is running?

  1. how do I replace the bash input used in that script with a simple hard coded command to the python script? Is the below attempt correct?
  2. since it's using venv is there anything extra I need to do when I check the instance and then kill it if it's there?

BASECMD=${1%%\ *}

#is this correct?
PID=$("python3 ./AI/ComfyUI/main.py" "$BASECMD")

if [ "$?" -eq "0" ]; then
   echo "at least one instance of "$BASECMD" found, killing all instances"

   # stop ComfyUI
   kill $PID
else
   echo "no running instances of "$BASECMD" found, starting one"

   # activate the venv
   source ./AI/ComfyUI/venv/bin/activate

   # start ComfyUI
   $1

fi

Last (minor) detail, for the .desktop shortcut I point to an icon. If possible I'd like to toggle icons for the start/stop states (like the image below). Is this possible with a .desktop?

enter image description here

1 Answer 1

1

This should work for you. Just modify the path's to the appropriate locations.

#!/bin/bash

pid_file="/tmp/comfyui.pid"
script_pid=""

update_icon() {
    if [ -z "$script_pid" ]; then
        # Not running, use inactive icon
        sed -i 's|^Icon=.*|Icon=/path/to/icon-inactive.png|' ~/Desktop/comfyui.desktop
    else
        # Running, use active icon
        sed -i 's|^Icon=.*|Icon=/path/to/icon-active.png|' ~/Desktop/comfyui.desktop
    fi
}

source /absolute/path/to/AI/ComfyUI/venv/bin/activate

# Check if file exists and read the PID
if [ -f "$pid_file" ]; then
    script_pid=$(cat "$pid_file")
fi

# Check if the process is running
if [ -n "$script_pid" ] && kill -0 "$script_pid" 2>/dev/null; then
    kill "$script_pid"
    wait "$script_pid"
    rm "$pid_file"      # Remove the PID file
    script_pid=""       # Clear the PID
else
    # If not running, start ComfyUI
    python3 /absolute/path/to/AI/ComfyUI/main.py &
    script_pid=$!
    echo "$script_pid" > "$pid_file"  # Save the PID to a file
fi

update_icon

deactivate

exit 0

The ~/.local/share/applications/comfyui.desktop file:

[Desktop Entry]
Version=1.0
Name=ComfyUI
Exec=bash -c "/absolute/path/to/my_script.sh"
Icon=/path/to/icon-inactive.png
Type=Application
Terminal=false

Copy it to your Desktop so it can be visible and you can freely change the .desktop file without affecting the default one stored in ~/.local:

cp ~/.local/share/applications/comfyui.desktop ~/Desktop/comfyui.desktop
4
  • @JayCravens thank's for your answer but I'm confused about a detail here. The .desktop file is calling the python script /path/to/AI/ComfyUI/main.py, that should be the bash script you have included above, but then that bash script also calls the same python script - that can't be right. Surely the bash script is being missed out? I need it to be ComfyUI.desktop -> ComfyUI-bash.sh -> main.py (main.py is part of ComfyUI so shouldn't be modified). Commented Sep 21, 2024 at 15:49
  • @garrettlynchirl It's just about using absolute path's, as terdon pointed out. I updated the scripts to reflect it. You are correct, it should be the /path/to/my_script.sh on the .desktop file.
    – JayCravens
    Commented Sep 21, 2024 at 15:57
  • @garrettlynchirl I changed the icon update to use the existing variable to be more reliable.
    – JayCravens
    Commented Sep 21, 2024 at 16:13
  • Comments have been moved to chat; please do not continue the discussion here. Before posting a comment below this one, please review the purposes of comments. Comments that do not request clarification or suggest improvements usually belong as an answer, on Unix & Linux Meta, or in Unix & Linux Chat. Comments continuing discussion may be removed.
    – terdon
    Commented Sep 21, 2024 at 16:42

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.