[Help Request] Running Scripts from an Application
February 04, 2020 03:36PM
Shairport-sync is a service/app that acts as an audio receiver and allows Apple devices to Airplay (stream) audio over wifi. I wrote a how-to on this forum on getting it set up on a Pogoplug. It runs at startup and is always available for streaming. It also allows you to run scripts when audio is initially streamed to it, as well as when audio stops being streamed to it. The scripts (detailed below) are intended to stop and start a process that captures audio from the microphone (input) on a USB sound card, and play it through the headphone (output) jack. These scripts work properly when run as the root user, but fail to run via Shairport-sync.

I used a guide as a reference for the setup I’m attempting below: https://www.hifiberry.com/docs/projects/airplay-player-with-line-input/ (there are some notes at the bottom about not using sudo in the scripts. I’m not doing this, but I didn’t install Shairport-sync by compiling manually as the tut did.) I also don't have sudo on my Pogoplug

Note from the developer of Shairport-sync about how it runs in a "weird environment" (but this note was for Debian Wheezy OS and I'm on Stretch).

The scripts:
Located in /root (root user folder … maybe this is the issue)

shairportstart.sh - stop the mic > headphone process
#!/bin/sh
/usr/bin/pkill arecord&
exit 0


shairportend.sh - start the mic > headphone process
#!/bin/sh
/usr/bin/arecord -D plughw:CARD=Set -f dat | /usr/bin/aplay -D plughw:CARD=Set -f dat&
exit 0


Shairport-sync Configuration:
Located at /etc/shairport-sync.conf
Relevant section for scripts:
// Advanced parameters for controlling how a Shairport Sync runs
sessioncontrol = 
{
	run_this_before_play_begins = "/root/shScripts/shairportstart.sh"; // make sure the application has executable permission. If it's a script, include the #!... stuff on the first line
	run_this_after_play_ends = "/root/shScripts/shairportend.sh"; // make sure the application has executable permission. It it's a script, include the #!... stuff on the first line
	wait_for_completion = "yes"; // set to "yes" to get Shairport Sync to wait until the "run_this..." applications have terminated before continuing
//	allow_session_interruption = "no"; // set to "yes" to allow another device to interrupt Shairport Sync while it's playing from an existing audio source
//	session_timeout = 120; // wait for this number of seconds after a source disappears before terminating the session and becoming available again.
};


Permissions of Scripts directory:
root@pogoplugpro:~/shScripts# ls -la
total 24
drwxrwxrwx 2 root           root           4096 Jan 19 22:01 .
drwx------ 6 root           root           4096 Jan 19 22:10 ..
-rwxrwxrwx 1 shairport-sync shairport-sync  104 Dec 29 13:56 shairportend.sh
-rwxrwxrwx 1 root           root            118 Dec 29 13:57 shairportfade.sh
-rwxrwxrwx 1 shairport-sync shairport-sync   41 Dec 29 14:24 shairportstart.sh
-rwxrwxrwx 1 shairport-sync shairport-sync  166 Jan 19 22:01 shairporttest.sh


User info:
root@pogoplugpro:~/shScripts# getent passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
sshd:x:101:65534::/var/run/sshd:/usr/sbin/nologin
messagebus:x:102:103::/var/run/dbus:/bin/false
avahi:x:103:104:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
ntp:x:104:106::/home/ntp:/bin/false
statd:x:105:65534::/var/lib/nfs:/bin/false
uuidd:x:100:101::/run/uuidd:/bin/false
_apt:x:106:65534::/nonexistent:/bin/false
systemd-timesync:x:107:109:systemd Time Synchronization,,,:/run/systemd:/bin/false
systemd-network:x:108:110:systemd Network Management,,,:/run/systemd/netif:/bin/false
systemd-resolve:x:109:111:systemd Resolver,,,:/run/systemd/resolve:/bin/false
systemd-bus-proxy:x:110:112:systemd Bus Proxy,,,:/run/systemd:/bin/false
shairport-sync:x:111:113::/run/shairport-sync:/bin/bash
motion:x:112:114::/var/lib/motion:/bin/false


I added /bin/bash as the shell for the shairport-sync user in a troubleshooting attempt that didn’t work. I believe it was previously /usr/sbin/nologin

Some ideas I've had more recently.
  • move scripts into a different location ... but where?
  • add the commands in the script directly to the config file ... not sure if they are too complex

Thanks if you've read this far! Double thanks if you have any ideas where I went awry.
Re: [Help Request] Running Scripts from an Application
February 04, 2020 04:54PM
Put it somewhere else, such as /usr/local/bin.

Generally, to execute a script the user needs some access to the entire path from / to where the script is, since that user won't have any access to /root this won't work. Rather than modify /root I'd suggest moving the scripts.
Re: [Help Request] Running Scripts from an Application
February 04, 2020 08:53PM
1000001101000 Thank you! You hit the nail on the head. I moved it to the /usr/local/bin/ directory and changed the owner to shairport-sync and everything works!

Do you know a good way to check if a user has access to the full path of a script? I'd like to have a better grasp of permissions in general, but a piece of knowledge at a time is better than none :)
Re: [Help Request] Running Scripts from an Application
February 04, 2020 11:10PM
I imagine there are a lot of good tutorials out there for learning about permissions, I don’t have a particular one to recommend. It’s important to learn but also difficult to keep straight sometimes.

Amusingly, the website for the class where I first learned about Unix permissions ~20 years ago is still online. It gives a pretty good overview of file permissions though not this specific issue:
https://www.december.com/unix/tutor/permissions.html
Re: [Help Request] Running Scripts from an Application
March 02, 2020 02:13PM
Reopening this topic with a new predicament.

When I stream music via shairport-sync and then stop that steaming, the script to switch over and record/output my mic (line-in) input works perfectly. However after an uncertain amount of time, the record/output process stops running and I can no longer play my auxiliary sound source without starting and stoping another shairport-sync stream (which just starts the record/output process again).

What is a good way to keep the record/output process persistent, or find out what's killing it? Shairport-sync should be the only thing that's killing it, but that's not the case, and I can also confirm when I'm done streaming it is starting the process again.
Re: [Help Request] Running Scripts from an Application
March 02, 2020 03:18PM
Does dmesg provide any clues? I've had the OOM (out of memory) process killer kill things on me.
Re: [Help Request] Running Scripts from an Application
March 02, 2020 03:27PM
If you mean checking the output of the command "dmesg" then no, there's no reference to the following processes which Shairport-sync is starting.

/usr/bin/arecord -D plughw:CARD=Set -f dat | /usr/bin/aplay -D plughw:CARD=Set -f dat

If there's some more specific command to try please let me know.
Re: [Help Request] Running Scripts from an Application
March 02, 2020 03:36PM
That's too bad. If it was getting killed there would be a slew of obvious ugly messages in the dmesg output. Do any of these processes create anything in /var/log?
cde
Re: [Help Request] Running Scripts from an Application
March 03, 2020 07:51PM
Typically, if it's in /usr/local/ whatever, the owner should be root (and group more often than not should be root too) and permissions set to rwxr-xr-x (755). The shairport-sync user should still be able to use those like that.

To check for messages, "dmesg" for the kernel log, which is on a circular buffer. Same thing with the syslog, use "logread | grep -v dhclient" (to ignore dhcp errors/messages). The problem is that these are circular buffers so when they get filled up the oldest message gets overwritten, especially when dhclient is SOOO noisy. That's the flaw of these small, hard drive less devices where we have to use volatile logging to protect the flash drives.

You may need to point it to a network server, increase the ring buffer size, or log to a file. You can edit /etc/default/busybox-syslogd based on the busy box syslogd man page https://manpages.debian.org/jessie/busybox-syslogd/syslogd.8.en.html
Just edit it back afterwards if you need to, especially you are on a flash drive and logging to file.
Re: [Help Request] Running Scripts from an Application
March 06, 2020 10:45AM
@renojim - thanks for the idea, but alas there is nothing about the aplay/arecord processes in /var/log

@cde - I changed ownership (permissions were already 755 on all scripts) to root and let the group default to the user login group (which was also root). After restarting the Shairport-sync service everything still runs. TBD how long it keeps running, but I'll see if I can find more info using your logging tips. Thank you!
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: