title: Bash Scripting - user interaction when script launched from Systemd
body: <p>I have been google searching a lot but I'm not finding any info to help me with my task.  Maybe its not possible.  I am working with Raspbian distro on a raspberry Pi but I think its relevant to anyone bash scripting.</p>
  
  <p>I have created udev rules and a systemd service that execute a bash script I wrote when the system detect a google phone plugged into usb (for example plugging in a nexus 4, 5 6P, etc).  And it all works.  You plug in a phone and my script will execute and use fastboot to obtain the device product name and then flash the appropriate twrp image to the recovery partition on phone (twrp being a custom made recovery os for the phone).</p>
  
  <p>If I execute the bash script manually at terminal you will have user interaction.  Echoing info and what not.  I have a 5sec count down timer gives the user the chance to stop execution after device is detected.  That works too.</p>
  
  <p>My problem is this:  When the script is executed by systemd its in it own virtual terminal or whatever so if your at a console you wont see anything from the script and the script cant see any user input (e.g. key stroke) to cancel out.</p>
  
  <p>I want to know, is there a way if your sitting at a console for my script to start displaying the 5 sec countdown warning and allow the user to cancel?</p>
  
  <p>I currently have the raspberry pi setup in headless mode so it boots to console and thats it.  It does not start any graphical environment.  And I usually ssh into the raspberry PI to work with it.  So when I ssh in and execute the w command I see my tty is /pts/0.</p>
  
  <p>Is it possible to get my script to output and read for any key stroke from a current console?  I hope that makes sense.</p>
  
  <p>I ended up making a function in my script that I can call to echo out to all active, open tty's but I have no idea how to read in.  And I figure there is probably a much more elegant way to achieve what I am doing.  Thanks in advance for any help or advice anyone could provide.  I'll put code and outputs below.</p>
  
  <p>Paul.</p>
  
  <pre><code>USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
  pi       tty1                      18:22    1:08m  1.18s  0.92s -bash
  pi       pts/0    fe80::187c:321f: 18:49    1.00s  2.98s  0.05s w
  </code></pre>
  
  <p>My udev rule</p>
  
  <pre><code># Google
  SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE:="0666", GROUP:="plugdev", TAG+="systemd", ENV{SYSTEMD_WANTS}+="load-fastboot-flasher.service"
  </code></pre>
  
  <p>My systemd service unit file</p>
  
  <pre><code>[Unit]
  Description=Google Device TWRP Recovery
  After=multi-user.target
  
  [Service]
  Type=idle
  User=pi
  ExecStart=/home/pi/fastboot-twrp/fastboot-twrp-flash.sh &gt; /dev/null &amp;
  StandardOutput=console
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>And finally my bash script</p>
  
  <pre><code>#!/bin/bash
  
  ######################################################################################
  # Our function that performs the flash after a device was detected.
  ######################################################################################
  fastboot-test() {
   GOOGLEDEVICENAME=`fastboot getvar product 2&gt;&amp;1 | grep "product:" | awk '{print $2}'`
   if [[ "$GOOGLEDEVICENAME" != "" ]]; then
      echoall "Device Product_Name: $GOOGLEDEVICENAME"
      if [ ! -d "$GOOGLEDEVICENAME" ]; then
         # Control will enter here if $DIRECTORY doesn't exist.
         echoall "Error!  Device folder &amp; recovery image dont exist.  Aborting!"
         echoall ""
         echoall "Please create a folder using the same product name as your device in the folder"
         echoall "that this script resides in and place your twrp image in this new folder."
         echoall "e.g. for a Nexus 4 make a folder called mako with your twrp image inside it."
         echoall ""
         exit 1
      fi
      cd $GOOGLEDEVICENAME
      echoall "Selecting twrp image: " $PWD/twrp*
      sleep 1
      echoall ""
      fastboot flash recovery $PWD/twrp*
      echoall ""
      echoall "twrp recovery image flash complete!"
      echoall "Enjoy your fishing.... ;-)"
      echoall ""
   else
      echoall "No connected Google devices"
   fi
  }
  
  ######################################################################################
  # Our function to print output to all currently open consoles (e.g. type w a terminal to see users)
  ######################################################################################
  echoall() {
   for PTS in $(w |grep -o pts/.) ; do echo $1 $2 $3 &gt;&gt;/dev/$PTS; echo $1 $2 $3 &gt;&gt;/home/pi/load-fastboot-flasher.log; done
  }
  
  
  
  ######################################################################################
  # START
  ######################################################################################
  #set +x
  echoall ""
  echoall "$(date)"
  echoall ""
  echoall "Google device detected.  Press C then ENTER to cancel TWRP recovery flash in 5 seconds"
  COUNT=5
  while (( COUNT &gt; 0 ))
   #This is our 5 sec timer.
   do
     read -t 1 -n 1 -r
     if [[ $REPLY == ^[Cc]$ ]]; then
        Exit 0
     fi
     sleep 1
     (( COUNT -- ))
     echoall $COUNT
   done
  echoall ""
  echoall ""
  #set -x
  
  DETECTED=`fastboot devices | awk '{print $2}'`
  if [[ "$DETECTED" == "fastboot" ]]; then
  
    #Save the current working directory
    RESTOREPATH=`pwd`
  
    #Change the current working directory to the directory that this script resides in
    cd $(dirname $(readlink -f $0))
    echoall "changed working directory to: "$PWD
    echoall ""
    #
  
    echoall "Device detected: Yes"
    fastboot-test
    cd $RESTOREPATH
    echoall "changed working directory to: "$PWD
    echoall ""
    echoall "### END TWRP FLASH ###"
    echoall ""
    for PTS in $(w |grep -o pts/.) ; do echo -ne '\n' &gt;&gt;/dev/$PTS; done
    exit
  else
    echoall "Error! Fastboot not properly detecting Google device."
  fi
  </code></pre>