CLI program not launching from systemd unit

So I made this twitch checker program and it is not launching the program streamlink it just exits with any errors

Code
#!/bin/bash

while getopts "sS:hD:m:q:" flag; do
  case $flag in
    s) # silent mode
    TWITCHONLINE_SILENT_MODE='thisisdoesntmeananythingsinceitonlychecksifthevariableexistssoimjustgoingtotellyoutowatchaZentreyastream'
    ;;
    h) # show help
      
    self_disable=TRUE
    echo "twitchonline: check if a twitch streamer is online
    -h            : show this help
    -S [streamer] : setup automatic stream checking for [streamer], see this apps code to change timing
    -D [streamer] : remove systemd units for automatic checking for [streamer]"
    ;;
    S) # generate files
      self_disable=TRUE
      # make sure directories exist
      mkdir "$HOME/.local/share/systemd/"
      mkdir "$HOME/.local/share/systemd/user/"
      
      # create service unit
      printf "[Unit]\nDescription=\"Execute twitchonline and check for $OPTARG\"\n[Service]\nType=oneshot\nExecStart=/bin/bash $HOME/.local/bin/twitchonline.sh $OPTARG\n[Install]\nWantedBy=twitchonline_$OPTARG.timer" > $HOME/.local/share/systemd/user/twitchonline_$OPTARG.service
      
      # create the timer unit
      FIRST_THIRD="[Unit]\nDescription=Run every 15 minutes at chosen times of day\n[Timer]\nOnCalendar="
      LAST_THIRD="\nAccuracySec=1min\nUnit=twitchonline_$OPTARG.service\n[Install]\nWantedBy=timers.target"
      printf "enter '1' for daytime\n      '2' for anytime\n      '3' for custom\n>>> "
      
      # lets take a card from the KDE playbook and make this program configurable
      read TIME_CHOICE
      case $TIME_CHOICE in
        1) OTHER_THIRD="7:00,7:15,7:30,7:45,8:00,8:15,8:30,8:45,9:00,9:15,9:30,9:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30" ;;
        2) OTHER_THIRD="0:00,0:15,0:30,0:45,1:00,1:15,1:30,1:45,2:00,2:15,2:30,2:45,3:00,3:15,3:30,3:45,4:00,4:15,4:30,4:45,5:00,5:15,5:30,5:45,6:00,6:15,6:30,6:45,7:00,7:15,7:30,7:45,8:00,8:15,8:30,8:45,9:00,9:15,9:30,9:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30,20:45,21:00,21:15,21:30,21:45,22:00,22:15,22:30,22:45,23:00,23:15,23:30,23:45" ;;
        3)
          printf "enter the times of day you want to check (in military time, seperatated by commas, do it this way or it wont work)\n>>> "
          read OTHER_THIRD
          if [ "$OTHER_THIRD" = "" ]; then
            echo "Error: input invalid"
            exit 1
          fi
        ;;
        *) echo "twitchonline: invalid option, please try again"; exit 1;;
      esac
      printf "${FIRST_THIRD}${OTHER_THIRD//,/\\nOnCalendar=}${LAST_THIRD}" > $HOME/.local/share/systemd/user/twitchonline_$OPTARG.timer
      
      printf "\nDo you want to open the stream in twitch (1) or streamlink (2)\n>>> "
      read chosen_client
      case $chosen_client in
        1) chosen_client="twitch";stream_qaulity="720p";;
        2) chosen_client="streamlink"
          printf "\nWhat qaulity do you want? 160p (1), 360p (2), 480p (3), 720p (4) is available sporadically--same with 720p60 (5), 1080p60 (6)\n>>> "
          read stream_qaulity
          case $stream_qaulity in
            1) stream_qaulity='160p';;
            2) stream_qaulity='360p';;
            3) stream_qaulity='480p';;
            4) stream_qaulity='720p';;
            5) stream_qaulity='720p60';;
            6) stream_qaulity='1080p60';;
            *) echo "twitchonline: invalid option, please try again"; exit 1;;
          esac
        ;;
        *) echo "twitchonline: invalid option, please try again"; exit 1;;
      esac
      
      # enable the modules for $OPTARG
      systemctl --user enable twitchonline_$OPTARG.timer
      systemctl --user start twitchonline_$OPTARG.timer
      systemctl --user enable twitchonline_$OPTARG.service
      systemctl --user daemon-reload
      
      printf "000\n$chosen_client\n$stream_qaulity" > "$(dirname "$(readlink -f $0)")/${OPTARG}stream_state.txt"
      
    ;;
    D) # delete files
      self_disable=TRUE
      echo $OPTARG
      
      # delete and disable modules for $OPTARG
      
      systemctl --user stop twitchonline_$OPTARG.timer
      systemctl --user stop twitchonline_$OPTARG.service
      systemctl --user disable twitchonline_$OPTARG.timer
      systemctl --user disable twitchonline_$OPTARG.service
      systemctl --user daemon-reload
      rm $HOME/.local/share/systemd/user/twitchonline_$OPTARG.timer
      rm $HOME/.local/share/systemd/user/twitchonline_$OPTARG.service
    ;;
    \?)
      self_disable=TRUE
      echo "twitchonline: invalid option, see help for instructions"
    ;;
  esac
done

function toconsole() {
  if [ -z "${TWITCHONLINE_SILENT_MODE}" ]; then
    echo $1
  fi
}

# chaos ensues
if [ -z "${self_disable}" ]; then
  sloc="$(dirname "$(readlink -f $0)")"
  todaysDateNumber=$(date '+%j')
  doesExist=$( sed '1!d' "$sloc/$1stream_state.txt" | grep -i -m 1 "$todaysDateNumber")
  mode="$(sed '2!d' "$sloc/$1stream_state.txt")";
  qaulity="$(sed '3!d' "$sloc/$1stream_state.txt")";
  
  if [ -z "${doesExist}" ]; then
    if ! grep "busy" "$sloc/$1prog_state.txt"; then
      printf "busy" >> "$sloc/$1prog_state.txt"
      wget "https://twitchtracker.com/$1" -O "$sloc/$1streamer.html" > /dev/null 2>&1
      if grep 'LIVE</span>' "$sloc/$1streamer.html"; then
          toconsole "stream found."
          if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
            case $mode in
              twitch) xdg-open "https://www.twitch.tv/$1/" ;;
              streamlink) DISPLAY=:0 streamlink "https://www.twitch.tv/$1/" $qaulity & ;;
              *) echo "twitchonline: unsupported mode, exiting";  exit 1 ;;
            esac
          fi
          streamstate="$(cat "$sloc/$1stream_state.txt")"
          yesterdaysDateNumber="$(sed '1!d' "$sloc/$1stream_state.txt")"
          rm "$sloc/$1stream_state.txt"
          printf "${streamstate//$yesterdaysDateNumber/$todaysDateNumber}"  >> "$sloc/$1stream_state.txt"
      else
        toconsole "No stream, exiting."
      fi
    else
      toconsole "program already active, exiting."
      exit 0
    fi
  else
    toconsole "already returned a stream for \"$1\" today"
  fi
  rm "$sloc/$1prog_state.txt" > /dev/null 2>&1
  rm "$sloc/$1streamer.html" > /dev/null 2>&1
fi
Configuration

[date]
[mode]
[qaulity]

163
twitch
360p
Systemd service
[Unit]
Description="Execute twitchonline and check for zentreya"

[Service]
Type=oneshot
ExecStart=/bin/bash /home/usr_40476/Projects/twitchOnline/twitchonline.sh zentreya

[Install]
WantedBy=twitchonline_zentreya.timer
Systemd timer
[Unit]
Description=Run every 15 minutes from 10:30 AM to 8:30 PM

[Timer]
OnCalendar=10:30
OnCalendar=10:45
OnCalendar=11:00
OnCalendar=11:15
OnCalendar=11:30
OnCalendar=11:45
OnCalendar=12:00
OnCalendar=12:15
OnCalendar=12:30
OnCalendar=12:45
OnCalendar=13:00
OnCalendar=13:15
OnCalendar=13:30
OnCalendar=13:45
OnCalendar=14:00
OnCalendar=14:15
OnCalendar=14:30
OnCalendar=14:45
OnCalendar=15:00
OnCalendar=15:15
OnCalendar=15:30
OnCalendar=15:45
OnCalendar=16:00
OnCalendar=16:15
OnCalendar=16:30
OnCalendar=16:45
OnCalendar=17:00
OnCalendar=17:15
OnCalendar=17:30
OnCalendar=17:45
OnCalendar=18:00
OnCalendar=18:15
OnCalendar=18:30
OnCalendar=18:45
OnCalendar=19:00
OnCalendar=19:15
OnCalendar=19:30
OnCalendar=19:45
OnCalendar=20:00
OnCalendar=20:15
AccuracySec=1min
Unit=twitchonline_zentreya.service

[Install]
WantedBy=timers.target

i tried replacing nohup with xdg-open and other things but it still wont work, what should i do?

You could start with explaining what you are trying to achieve.

so, the last chunk of code, (the part that does the checking,) work fine. But when streamlink is called it simply exits without any errors. but it should be opening a stream.

what streamlink does is it sends a stream to your chosen video player, in this case vlc but nothing happens.

to debug the program I set the first line of the streamstate file to a different number and then I run the following

usr_40476@localhost:~> systemctl start --user twitchonline_onigiri.service 
usr_40476@localhost:~> systemctl status --user twitchonline_onigiri.service 
○ twitchonline_onigiri.service - "Execute twitchonline and check for onigiri"
     Loaded: loaded (/home/usr_40476/.local/share/systemd/user/twitchonline_onigiri.service; enabled; preset: disabled)
     Active: inactive (dead) since Wed 2024-06-12 11:17:41 EDT; 1s ago
TriggeredBy: ● twitchonline_onigiri.timer
    Process: 13346 ExecStart=/bin/bash /home/usr_40476/.local/bin/twitchonline.sh onigiri (code=exited, status=0/SUCCESS)
   Main PID: 13346 (code=exited, status=0/SUCCESS)
        CPU: 2.000s

Jun 12 11:17:37 localhost.localdomain bash[13381]: [cli][info] Found matching plugin twitch for URL https://www.twitch.tv/onigiri/
Jun 12 11:17:38 localhost.localdomain bash[13381]: [cli][info] Available streams: audio_only, 160p (worst), 360p, 480p, 720p60, 1440p60 (best)
Jun 12 11:17:38 localhost.localdomain bash[13381]: [cli][info] Opening stream: 360p (hls)
Jun 12 11:17:38 localhost.localdomain bash[13381]: [cli][info] Starting player: /usr/bin/vlc
Jun 12 11:17:40 localhost.localdomain vlc[13387]: The Wayland connection experienced a fatal error: Invalid argument
Jun 12 11:17:41 localhost.localdomain bash[13381]: [cli][info] Player closed
Jun 12 11:17:41 localhost.localdomain bash[13381]: [cli][info] Stream ended
Jun 12 11:17:41 localhost.localdomain bash[13381]: [cli][info] Closing currently open stream...
Jun 12 11:17:41 localhost.localdomain systemd[2847]: Finished "Execute twitchonline and check for onigiri".
Jun 12 11:17:41 localhost.localdomain systemd[2847]: twitchonline_onigiri.service: Consumed 2.000s CPU time.

the notable thing here is

Jun 12 11:17:40 localhost.localdomain vlc[13387]: The Wayland connection experienced a fatal error: Invalid argument

changed the code a little,

new code
#!/bin/bash

while getopts "sS:hD:m:q:" flag; do
  case $flag in
    s) # silent mode
    TWITCHONLINE_SILENT_MODE='thisisdoesntmeananythingsinceitonlychecksifthevariableexistssoimjustgoingtotellyoutowatchaZentreyastream'
    ;;
    h) # show help
      
    self_disable=TRUE
    echo "twitchonline: check if a twitch streamer is online
    -h            : show this help
    -S [streamer] : setup automatic stream checking for [streamer], see this apps code to change timing
    -D [streamer] : remove systemd units for automatic checking for [streamer]"
    ;;
    S) # generate files
      self_disable=TRUE
      # make sure directories exist
      mkdir "$HOME/.local/share/systemd/"
      mkdir "$HOME/.local/share/systemd/user/"
      
      # create service unit
      printf "[Unit]\nDescription=\"Execute twitchonline and check for $OPTARG\"\n[Service]\nType=oneshot\nExecStart=/bin/bash $HOME/.local/bin/twitchonline.sh $OPTARG\n[Install]\nWantedBy=twitchonline_$OPTARG.timer" > $HOME/.local/share/systemd/user/twitchonline_$OPTARG.service
      
      # create the timer unit
      FIRST_THIRD="[Unit]\nDescription=Run every 15 minutes at chosen times of day\n[Timer]\nOnCalendar="
      LAST_THIRD="\nAccuracySec=1min\nUnit=twitchonline_$OPTARG.service\n[Install]\nWantedBy=timers.target"
      printf "enter '1' for daytime\n      '2' for anytime\n      '3' for custom\n>>> "
      
      # lets take a card from the KDE playbook and make this program configurable
      read TIME_CHOICE
      case $TIME_CHOICE in
        1) OTHER_THIRD="7:00,7:15,7:30,7:45,8:00,8:15,8:30,8:45,9:00,9:15,9:30,9:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30" ;;
        2) OTHER_THIRD="0:00,0:15,0:30,0:45,1:00,1:15,1:30,1:45,2:00,2:15,2:30,2:45,3:00,3:15,3:30,3:45,4:00,4:15,4:30,4:45,5:00,5:15,5:30,5:45,6:00,6:15,6:30,6:45,7:00,7:15,7:30,7:45,8:00,8:15,8:30,8:45,9:00,9:15,9:30,9:45,10:00,10:15,10:30,10:45,11:00,11:15,11:30,11:45,12:00,12:15,12:30,12:45,13:00,13:15,13:30,13:45,14:00,14:15,14:30,14:45,15:00,15:15,15:30,15:45,16:00,16:15,16:30,16:45,17:00,17:15,17:30,17:45,18:00,18:15,18:30,18:45,19:00,19:15,19:30,19:45,20:00,20:15,20:30,20:45,21:00,21:15,21:30,21:45,22:00,22:15,22:30,22:45,23:00,23:15,23:30,23:45" ;;
        3)
          printf "enter the times of day you want to check (in military time, seperatated by commas, do it this way or it wont work)\n>>> "
          read OTHER_THIRD
          if [ "$OTHER_THIRD" = "" ]; then
            echo "Error: input invalid"
            exit 1
          fi
        ;;
        *) echo "twitchonline: invalid option, please try again"; exit 1;;
      esac
      printf "${FIRST_THIRD}${OTHER_THIRD//,/\\nOnCalendar=}${LAST_THIRD}" > $HOME/.local/share/systemd/user/twitchonline_$OPTARG.timer
      
      printf "\nDo you want to open the stream in twitch (1) or streamlink (2)\n>>> "
      read chosen_client
      case $chosen_client in
        1) chosen_client="twitch";stream_qaulity="720p";;
        2) chosen_client="streamlink"
          printf "\nWhat qaulity do you want? 160p (1), 360p (2), 480p (3), 720p (4) is available sporadically--same with 720p60 (5), 1080p60 (6)\n>>> "
          read stream_qaulity
          case $stream_qaulity in
            1) stream_qaulity='160p';;
            2) stream_qaulity='360p';;
            3) stream_qaulity='480p';;
            4) stream_qaulity='720p';;
            5) stream_qaulity='720p60';;
            6) stream_qaulity='1080p60';;
            *) echo "twitchonline: invalid option, please try again"; exit 1;;
          esac
        ;;
        *) echo "twitchonline: invalid option, please try again"; exit 1;;
      esac
      
      # enable the modules for $OPTARG
      systemctl --user enable twitchonline_$OPTARG.timer
      systemctl --user start twitchonline_$OPTARG.timer
      systemctl --user enable twitchonline_$OPTARG.service
      systemctl --user daemon-reload
      
      printf "000\n$chosen_client\n$stream_qaulity" > "$(dirname "$(readlink -f $0)")/${OPTARG}stream_state.txt"
      
    ;;
    D) # delete files
      self_disable=TRUE
      echo $OPTARG
      
      # delete and disable modules for $OPTARG
      
      systemctl --user stop twitchonline_$OPTARG.timer
      systemctl --user stop twitchonline_$OPTARG.service
      systemctl --user disable twitchonline_$OPTARG.timer
      systemctl --user disable twitchonline_$OPTARG.service
      systemctl --user daemon-reload
      rm $HOME/.local/share/systemd/user/twitchonline_$OPTARG.timer
      rm $HOME/.local/share/systemd/user/twitchonline_$OPTARG.service
    ;;
    \?)
      self_disable=TRUE
      echo "twitchonline: invalid option, see help for instructions"
    ;;
  esac
done

function toconsole() {
  if [ -z "${TWITCHONLINE_SILENT_MODE}" ]; then
    echo $1
  fi
}

# chaos ensues
if [ -z "${self_disable}" ]; then
  sloc="$(dirname "$(readlink -f $0)")"
  todaysDateNumber=$(date '+%j')
  doesExist=$( sed '1!d' "$sloc/$1stream_state.txt" | grep -i -m 1 "$todaysDateNumber")
  mode="$(sed '2!d' "$sloc/$1stream_state.txt")";
  qaulity="$(sed '3!d' "$sloc/$1stream_state.txt")";
  
  if [ -z "${doesExist}" ]; then
    if ! grep "busy" "$sloc/$1prog_state.txt"; then
      printf "busy" >> "$sloc/$1prog_state.txt"
      wget "https://twitchtracker.com/$1" -O "$sloc/$1streamer.html" > /dev/null 2>&1
      if grep 'LIVE</span>' "$sloc/$1streamer.html"; then
          toconsole "stream found."
          if [ "$(notify-send "$1 is online!" "https://www.twitch.tv/$1/" -u CRITICAL -a "$1-detector" -A 'Open Stream' -A 'Nope')" -eq '0' ]; then
            case $mode in
              twitch) xdg-open "https://www.twitch.tv/$1/" ;;
              streamlink) nohup streamlink "https://www.twitch.tv/$1/" $qaulity & ;;
              *) echo "twitchonline: unsupported mode, exiting";  exit 1 ;;
            esac
            sleep 7
          fi
          streamstate="$(cat "$sloc/$1stream_state.txt")"
          yesterdaysDateNumber="$(sed '1!d' "$sloc/$1stream_state.txt")"
          rm "$sloc/$1stream_state.txt"
          printf "${streamstate//$yesterdaysDateNumber/$todaysDateNumber}"  >> "$sloc/$1stream_state.txt"
      else
        toconsole "No stream, exiting."
      fi
    else
      toconsole "program already active, exiting."
      exit 0
    fi
  else
    toconsole "already returned a stream for \"$1\" today"
    exit 0
  fi
  rm "$sloc/$1prog_state.txt" > /dev/null 2>&1
  rm "$sloc/$1streamer.html" > /dev/null 2>&1
fi
usr_40476@localhost:~> systemctl start --user twitchonline_onigiri.service 
usr_40476@localhost:~> systemctl status --user twitchonline_onigiri.service 
○ twitchonline_onigiri.service - "Execute twitchonline and check for onigiri"
     Loaded: loaded (/home/usr_40476/.local/share/systemd/user/twitchonline_onigiri.service; enabled; preset: disabled)
     Active: inactive (dead) since Wed 2024-06-12 11:23:31 EDT; 4s ago
TriggeredBy: ● twitchonline_onigiri.timer
    Process: 13722 ExecStart=/bin/bash /home/usr_40476/.local/bin/twitchonline.sh onigiri (code=exited, status=0/SUCCESS)
   Main PID: 13722 (code=exited, status=0/SUCCESS)
        CPU: 2.340s

Jun 12 11:23:23 localhost.localdomain bash[13754]: [cli][info] Found matching plugin twitch for URL https://www.twitch.tv/onigiri/
Jun 12 11:23:24 localhost.localdomain bash[13754]: [cli][info] Available streams: audio_only, 160p (worst), 360p, 480p, 720p60, 1440p60 (best)
Jun 12 11:23:24 localhost.localdomain bash[13754]: [cli][info] Opening stream: 360p (hls)
Jun 12 11:23:24 localhost.localdomain bash[13754]: [cli][info] Starting player: /usr/bin/vlc
Jun 12 11:23:25 localhost.localdomain vlc[13761]: Qt: Session management error: Could not open network socket
Jun 12 11:23:30 localhost.localdomain bash[13754]: [cli][info] Stream ended
Jun 12 11:23:31 localhost.localdomain bash[13754]: Interrupted! Exiting...
Jun 12 11:23:31 localhost.localdomain bash[13754]: [cli][info] Closing currently open stream...
Jun 12 11:23:31 localhost.localdomain systemd[2847]: Finished "Execute twitchonline and check for onigiri".
Jun 12 11:23:31 localhost.localdomain systemd[2847]: twitchonline_onigiri.service: Consumed 2.340s CPU time.
usr_40476@localhost:~> 
Jun 12 11:23:25 localhost.localdomain vlc[13761]: Qt: Session management error: Could not open network socket

I dont know what i did but now i have a different problem, starting a new thread.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.