Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run_this_before_play_begins doesn't work when running as service #152

Closed
john-a-harris opened this issue Nov 3, 2015 · 19 comments
Closed

Comments

@john-a-harris
Copy link

I have entries in my configuration to run a script before play starts and another after play finishes.
If I run shairport-sync as a service, these scripts do not get run. However, if I run shairport-sync at the command line (as root) the scripts are run as expected.
I am running on a raspberry pi, using raspbian wheezy. The scripts are located in a user's folder and have permissions set by chmod 755.
What could I be doing wrong here?

@mikebrady
Copy link
Owner

Try using full pathnames?

@john-a-harris
Copy link
Author

Hi - thanks for the reply. I am already using full pathnames. Here's the session control section in full:

sessioncontrol =
{
run_this_before_play_begins = "/home/pi/bin/led_on.sh"; // make sure the application has executable permission. It it's a script, include the #!... stuff on the first line
run_this_after_play_ends = "/home/pi/bin/led_off.sh"; // make sure the application has executable permission. It it's a script, include the #!... stuff on the first line
// wait_for_completion = "no"; // 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.
};

Any other suggestions gratefully accepted. It's a nice to have, rather than a must have for me, but does enable some cool stuff to happen, so it would be nice to have it working...

@mikebrady
Copy link
Owner

Thanks for the information. The environment that those scripts run in is a bit weird. Here is a little script that works in the service mode on the Raspberry Pi running Wheezy:

#!/bin/sh
/usr/bin/logger "Hi there from the Temporary Script"
PWD=`/bin/pwd`
/usr/bin/logger "The working directory is \"$PWD\" and the PATH variable is \"$PATH\"."

You'll see different results depending on whether Shairport Sync is run from a command line or as a service. Notice that both the working directory and the PATH are different, so it's probably best to use full path names for everything inside the script too.

@john-a-harris
Copy link
Author

Apologies for the misunderstanding. I see what you mean now. Your suggestion has resolved the issue. Thanks very much for all you've done on this project - it's helping me do multi-room audio for a song!

@mikebrady
Copy link
Owner

Thanks. Actually, I coulda been a bit more explicit in what I wrote...

@chasylexus
Copy link

Hi mikebrady!
Thank you for your amazing work!
Unfortunately I also have some problem with run_this_before_play_begins and run_this_after_play_ends with shairport-sync (run on startup using systemd) working on Raspbian Jessie.
Seems it doesn't work. Always used full path:

/etc/shairport-sync.conf:

...
run_this_before_play_begins = "/usr/local/bin/squeezelite_stop";
run_this_after_play_ends = "/usr/local/bin/squeezelite_start";
...

script /usr/local/bin/squeezelite_stop:

#!/bin/bash
sudo /etc/init.d/squeezelite stop

script /usr/local/bin/squeezelite_start:

#!/bin/bash
sudo /etc/init.d/squeezelite start

If I begin to stream sound to Raspberry via shairport-sync, squeezelite doesn't stop. If I run these scripts manually, squeezelite stops and starts. What's wrong with it?

@mikebrady
Copy link
Owner

Thanks for your kind words! It sounds like a problem due to the different environment the scripts run in when they are launched by Shairport Sync.

The scripts will be running under the user shairport-sync. Is the user shairport-sync in the sudo group? Another issue is that many of the PATH definitions will be different.

See some of the previous comments and #152 (comment) for a script to do some experimentation.

@chasylexus
Copy link

Shairport-sync wasn't in sudo group but I've repaired this and now it is OK:
uid=999(shairport-sync) gid=996(shairport-sync) groups=996(shairport-sync),27(sudo),29(audio).
Unfortunately I'm not familiar with Linux and it is not clear for me what's wrong with it if I used full path in all scripts. As for #152 (comment) unfortunately I don't understand what I have to do: is it script that should run at startup? What kind of information should I receive using it?

@chasylexus
Copy link

Could you post here please an example of working run_this_before_play_begins or run_this_before_play_ends parameters?

@mikebrady
Copy link
Owner

As for #152 (comment) unfortunately I don't understand what I have to do: is it script that should run at startup? What kind of information should I receive using it?

That is an example of a script that could be a run_this_before_play_begins script or run_this_after_play_ends script. It just puts a few lines in the log file.

The scripts you list above do not give a full path name for sudo – do $which sudo to get its full path name.

Beyond this, I'm afraid you'll have to learn about Linux elsewhere.

@chasylexus
Copy link

Thank you very much for your answer! I'll try this and defenitely will learn more about Linux.

@wcsuther
Copy link

chasylexus, did you figure out a solution to this. I am trying to do the same thing. I had it working in Wheezy with simple scripts like you have listed above, inserted in the start-stop-daemon with -B and -E flags. I am assuming you had the same setup? I've no luck in Jessie thus far...my knowledge of linux is probably similar to yours...

Thanks. (Sorry to hijack this thread Mike...)

@chasylexus
Copy link

wcsuther, yes I solved this but now I don't use this.
As I remember, I used something like /usr/bin/sudo instead sudo in my scripts, so script /usr/local/bin/squeezelite_stop looks like

#!/bin/bash
/usr/bin/sudo /etc/init.d/squeezelite stop

You can obtain right substitution for sudo by executing of this command:
which sudo as Mike listed above.

Good luck!

@wcsuther
Copy link

I tried that, but to no avail. I added user shairport-sync to the sudo group, ran which sudo, and edited my script like you did above. I then added to /etc/shairport-sync.conf:

sessioncontrol =
{
run_this_before_play_begins = "/usr/local/bin/squeezelite_stop"; // make sure the application has executable permission. It it's a script, include the #!... stuff on the first line
run_this_after_play_ends = "/usr/local/bin/squeezelite_start"; // make sure the application has executable permission. It it's a script, include the #!... stuff on the first line

Still no luck. I'll keep digging, thanks.

@chasylexus
Copy link

As I remember I also added shairport-sync to all groups that included pi user.

@rkam
Copy link

rkam commented Feb 29, 2016

Are you logged in as the shairport-sync user when trying your scripts? If not, do that. - Since that's the environment the script runs in, you can find and fix a lot of errors and it's much easier that way. (One way to do that is to do this "sudo su - shairport-sync")

Apologies if you've done this, I haven't been following this thread.

I just had a thought that maybe sudo needs a password.

You can more easily check if something isn't in the PATH too (though you may have found all the cases already).

@p3ck
Copy link

p3ck commented Feb 29, 2016

I am using this feature to mute/unmute an amplifier... here is a snippet
from my shairport-sync config

run_this_before_play_begins = "/usr/bin/sudo /usr/local/bin/unmute"
run_this_after_play_ends = "/usr/bin/sudo /usr/local/bin/mute"
wait_for_completion = "yes"

and my sudo entry...
cat sudoers.d/shairport-sync
shairport-sync ALL=NOPASSWD: /usr/local/bin/unmute, /usr/local/bin/mute

This works perfect for me.

On Mon, Feb 29, 2016 at 2:37 PM, rkam notifications@github.com wrote:

Are you logged in as the shairport-sync user when trying your scripts? If
not, do that. - Since that's the environment the script runs in, you can
find and fix a lot of errors and it's much easier that way. (One way to do
that is to do this "sudo su - shairport-sync")

Apologies if you've done this, I haven't been following this thread.

I just had a thought that maybe sudo needs a password.

You can more easily check if something isn't in the PATH too (though you
may have found all the cases already).


Reply to this email directly or view it on GitHub
#152 (comment)
.

Bill Peck
http://www.pecknet.com

@wcsuther
Copy link

wcsuther commented Mar 1, 2016

Thank you chasulexus, rkam, and p3ck...it was a password issue. I hadn't set my /etc/sudoers correctly. It's working flawlessly now switching between squeezelite and shairport-sync... Thanks again.

@RandScullard
Copy link

RandScullard commented Oct 1, 2016

Thanks to everyone on this thread, I was able to get shairport-sync to stop the motion service while music is playing and start it again when the music stops. (My Pi doesn't have enough CPU power to keep the music playing glitch free when motion is active.) In case it helps anyone else, here are the settings I used:

run_this_before_play_begins = "/usr/bin/sudo service motion stop";
run_this_after_play_ends = "/usr/bin/sudo service motion start";

And I also needed to add shairport-sync to my sudoers:

shairport-sync  ALL=(ALL) NOPASSWD: ALL

@mikebrady shairport-sync is a beautiful piece of work!

Thanks again to all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants