Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

sound not playing at crontab -e

Sat Apr 11, 2015 7:56 pm

I did add a sound to a python file to play.

Code: Select all

pygame.mixer.init()
pygame.mixer.music.load("ping.wav")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy() == True:
    continue
This plays fine if I do run by hand:

Code: Select all

sudo python tuitlezer.py
If I do run this file by crontab -e the sound is not played.
The crontab command:

Code: Select all

*/2 * * * * /usr/bin/python /home/ewh/tuitlezer.py >/dev/null 2>&1
The ">/dev/null" is to prevend sending mail every 2 minutes


Why is thye sound not played by the crontab?

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: sound not playing at crontab -e

Sat Apr 11, 2015 8:46 pm

If your script needs root, did you add it to the root crontab?
There are 10 types of people: those who understand binary and those who don't.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: sound not playing at crontab -e

Sat Apr 11, 2015 9:07 pm

Or, more to the point, if the command line version requires "sudo", then so also will the crontab version.

Note that this (keeping it in the user 'pi' crontab, but using sudo) is cleaner than the alternative of putting it in the root crontab.

The cleanest solution would be to make the script setuid root, then you wouldn't need sudo at all.

Note 1: I know nothing about Python. I don't know if it is (directly) possible to make a Python script setuid root.

Note 2: I said "directly" in the previous sentence, because there's usually a workaround - a way to get setuid even if the designers of the langauge don't want you to do it. (Shell scripts are a case-in-point)
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: sound not playing at crontab -e

Sat Apr 11, 2015 9:08 pm

kusti8 wrote:If your script needs root, did you add it to the root crontab?

Yes I did

Canedje
Posts: 265
Joined: Thu Mar 26, 2015 7:18 am

Re: sound not playing at crontab -e

Sat Apr 11, 2015 9:13 pm

Canedje wrote:
kusti8 wrote:If your script needs root, did you add it to the root crontab?

Yes I did

You triggered me with the root.

The path off the root was not from the root in the python source.
I changed it. Now it workes.
Thanks for the tip

User avatar
DougieLawson
Posts: 39121
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: sound not playing at crontab -e

Sat Apr 11, 2015 9:16 pm

You'd have to make python setuid because it's an interpreted language not one that's compiled.

I think the cleanest option is to use sudo in the shebang line

Code: Select all

#!/usr/bin/sudo /usr/bin/python
instead of

Code: Select all

#!/usr/bin/python
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: sound not playing at crontab -e

Sat Apr 11, 2015 9:28 pm

#!/usr/bin/sudo /usr/bin/python
Yup, that should work.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Return to “Troubleshooting”