Bash script to start/stop ComfyUI

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