Nohup not working as expected

When I am running a command with nohup from a bash script in a systemd unit and the nohup part is not working

basically the program is being killed when the systemd unit exits, what should I do?

this is the command

nohup streamlink "https://www.twitch.tv/$1/" $qaulity &

Wher is this to be found outside any context? Provide full path.

it is inside a bash script
would you like to see the systemd config?

#!/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 &
                sleep 15
              ;;
              *) 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"
    exit 0
  fi
  rm "$sloc/$1prog_state.txt" > /dev/null 2>&1
  rm "$sloc/$1streamer.html" > /dev/null 2>&1
fi

And …?

No.

I only saw this lonely call of a program. Outside a precise defined environment like a logged in session that defines PATH, like cron , at nohuband systemd you are never sure and thus use complete path’s for everyhing.

And again, no, I am not going to read or trying to understand your script. I have even no idea what your goal is. This I can not say any useful thing about it.

the programs checks if a twitch streamer is online and opens the chosen application, I will also add that the program only has this issue when launched from a systemd unit, and behaves exactly as expected when ran from a terminal session.

Well nohup ensures a process is not affected by the SIGHUP signal . A systemd unit is terminated with SIGKILL by default and if the process is still alive after a timeout then SIGKILL is used to finish the unit process finally. See

> man systemd.kill

So if you use default settings in your systemd unit for KillSignal, SendSIGKILL and so on of course nohup will not prevent the termination of your process.
I must admit that i am not quite sure what you want to achive. The streamlink process should persist when the unit is shutdown ?

yes, the streamlink should stay running when the systemd unit exits

so apparently adding KillMode=process to the unit fixes this.

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