'4 wrote: > 4;1008830']Retrieving the PID is obviously not going to work, because > the status message doesn't contain the PID. So I changed it into this: > > Code: -------------------- > > # determine cardname from squeezelite process id > local PID=$(ps ax | pgrep squeezelite) > local cardname=$(ps | grep $PID | awk -F CARD= {'print $2'} | awk -F , {'print $1'}) -------------------- > > > > I might also need to change the command to get the card name, but I > don't have time right now to test it It didn't work so had tp change it to:
Code: -------------------- # determine card name from squeezelite process local cardname=$(ps ax | grep squeezelite | awk -F CARD= {'print $2'} | awk -F , {'print $1'}) -------------------- In order to extract the card name from the squeezite process I also needed to start sqeezelite with custum parameters by editting '/etc/systemd/system/squeezelite.service.d/dietpi-services_edit.conf'. Otherwise it would start it without the -o option. Code: -------------------- [Unit] #Description=SqueezeLite (DietPi) [Service] #User=squeezelite ExecStart= ExecStart=/usr/bin/squeezelite -o hw:CARD=C20,DEV=0 -a 4096:8096:32:0 -C 5 -n "DietPi-Squeezelite" -f /var/log/squeezelite.log -D dop [Install] #WantedBy=multi-user.target -------------------- This is the modified script for DietPi (dislaimer: not fully tested yet) Code: -------------------- #!/bin/sh # Script to be used by udev rules that detect when a USB DAC is connected or disconnected, # in order to start or stop Squeezelite # $1 : is the script option: 'restart', 'stop', 'find' or 'make' # $2 : For 'restart' and 'stop', $2 is the kernel name ($kernel) that is generated by the udev event # It is used to match a 'removed' USB device against the device assigned when the DAC was inserted # For 'find' and 'make', $2 is the (CARD)name of the DAC, as used in the squeezelite command string. # The 'find' option is used at boot to detect which device the DAC is connected to, # in the case that the DAC is up before squeezelite attempts to start. # The 'make' option creates the udev rules file # Examples: # sqlite_control make DAC # This will create a rules file in /etc/udev/rules.d. # The rule number is curently fixed at 10, and the rule name will equal the card name; in this case 'DAC' # Run this once, manually from the command line. # sqlite_control find DAC # This will check whether the DAC is connected, and write its kernel id to /tmp/DAC_kername.txt # This is intended to be used as a User Command, run at boot, so that the 'stop' option knows which kernel id to look for. # sqlite_control restart $kernel and sqlite_control stop $kernel # (Re)start and stop squeezelite, respectively. # These two options are called by the udev rules in response to 'add' and 'remove' events # They take the $kernel argument that is available when the udev rule is triggered. # The associated udev rules are of the form: # SUBSYSTEM=="usb", ACTION=="add", ATTR{idVendor}=="xxxx", ATTR{idProduct}=="xxxx", RUN+="/home/sqlite/sqlite_control.sh restart $kernel" # SUBSYSTEM=="usb", ACTION=="remove", RUN+="/home/sqlite/sqlite_control.sh stop $kernel" SQLITErestart() { local logfile=$1 local attempts=5 # number of tries local interval=1 # interval between tries (seconds) local uptime local count=$attempts while [ "$(dietpi-services status squeezelite)" == *"inactive (dead)"* ]; do if [ $((count--)) -le 0 ]; then uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})]) echo "$uptime Squeezelite failed to initialize within $attempts attempts." >> $logfile exit 1 fi uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})]) echo "$uptime Attempting to start squeezelite" >> $logfile dietpi-services restart squeezelite >> $logfile sleep $interval done # determine cardname from squeezelite process local cardname=$(ps ax | grep squeezelite | awk -F CARD= {'print $2'} | awk -F , {'print $1'}) sudo /usr/sbin/alsactl restore $cardname } logfile=/var/log/dietpi_dac.log case $1 in restart ) # restart squeezelite when DAC is inserted/powered up # output the device '$kernel' name to a text file, so that it can be used to check against USB 'remove' events kername=$2 uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})]) if [ -z $kername ]; then echo "$uptime DAC not detected" >> $logfile else echo "$uptime DAC detected on $kername" >> $logfile echo $kername > /tmp/DAC_kername.txt SQLITErestart $logfile & fi ;; stop ) # stop squeezelite when DAC is removed/powered down # compare $kernel device name to the one created by this script when the DAC was inserted/powered up startname=$(cat /tmp/DAC_kername.txt) stopname="$2" if [ $stopname == $startname ]; then # stop squeezelite if names match uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})]) echo "$uptime DAC on $stopname removed" >> $logfile dietpi-services stop squeezelite >> $logfile fi ;; find ) # create the file with the kernel name # find Vendor and Product IDs for the DAC from the udev rules cardname=$2 # name the rules file after the (CARD)name of the DAC rulesfile=etc/udev/rules.d/10-$cardname.rules idVendor=$(cat /$rulesfile | grep idVendor | awk -F idVendor {'print $2'} | awk -F \" {'print $2'}) idProduct=$(cat /$rulesfile | grep idProduct | awk -F idProduct {'print $2'} | awk -F \" {'print $2'}) uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})]) echo "$uptime Searching for $cardname with idVendor=$idVendor and idProduct=$idProduct in dmesg" >> $logfile kername=$(dmesg | grep -m 1 "idVendor=$idVendor, idProduct=$idProduct" | awk -F usb {'print $2'} | awk -F : {'print $1'}) # output the $kernel device name to a file for use when detecting 'removed' USB devices echo $kername > /tmp/DAC_kername.txt uptime=$(printf [%12s $(cat /proc/uptime | awk {'print $1'})]) if [ -z $kername ]; then echo "$uptime $cardname not detected" >> $logfile else echo "$uptime $cardname detected on $kername" >> $logfile sudo /usr/sbin/alsactl restore fi ;; make ) # make the rules file # one command line parameter: the name of the 'CARD', as used in the squeezelite command line scriptfile=$(realpath $0) cardname=$2 # look for cardname in /proc/asound/cards to determine card number cardno=$(cat /proc/asound/cards | grep -m 1 $cardname | awk {'print $1'}) # extract idVendor and idProduct from /proc/asound/card<n>/usbid usbid=$(cat /proc/asound/card$cardno/usbid) idVendor=$(echo $usbid | awk -F: {'print $1'}) idProduct=$(echo $usbid | awk -F: {'print $2'}) # create udev rules rule_add='SUBSYSTEM=="usb", ACTION=="add", ATTR{idVendor}=="'$idVendor'", ATTR{idProduct}=="'$idProduct'", RUN+="'$scriptfile'$ rule_remove='SUBSYSTEM=="usb", ACTION=="remove", RUN+="'$scriptfile' stop $kernel"' # create rules file rulesfile=etc/udev/rules.d/10-$cardname.rules echo $rule_add > /$rulesfile echo $rule_remove >> /$rulesfile ;; esac -------------------- ------------------------------------------------------------------------ 4]4's Profile: http://forums.slimdevices.com/member.php?userid=71351 View this thread: http://forums.slimdevices.com/showthread.php?t=113661 _______________________________________________ unix mailing list unix@lists.slimdevices.com http://lists.slimdevices.com/mailman/listinfo/unix