CapnWinky07
Posts: 12
Joined: Thu Oct 26, 2017 5:39 pm

Crontab script works in PI3 but not Zero [solved]

Mon Oct 30, 2017 7:29 pm

Hello all Racking my brain here. I followed an instructional online for a project I'm doing. I wrote a post about it before but it's basically a portable voice changer for a Halloween costume. It's not overly complicated. I followed the instructions to the T in this post:

https://www.raspberrypi-spy.co.uk/2017/ ... y-pi-zero/

The issue is my crontab script won't kick off the script on my Pi0. The weird thing is...if I take the sim card out and put it in the Pi3 it works fine. Furthermore, in the Pi0, I can run the contents of my crontab script manually and everything works fine.

Since it works in the Pi3, I can't imagine it's an error on my part. Both units are reading off the same SD card with the same exact cords/USB audio card. Only thing that is different is the USB micro to USB converter.

Thoughts?
Last edited by CapnWinky07 on Tue Oct 31, 2017 9:44 pm, edited 1 time in total.

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

Re: Crontab script works in PI3 but not Zero

Mon Oct 30, 2017 8:02 pm

Update your crontab entry so that it logs stdout and stderr to a file.

Code: Select all

13 22 * * 3 /home/pi/scripty_thing.py  >/tmp/scripty_log.txt 2>&1
After you think the script should have run take a look at /tmp/scripty_log.txt and see what errors occurred.
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.

CapnWinky07
Posts: 12
Joined: Thu Oct 26, 2017 5:39 pm

Re: Crontab script works in PI3 but not Zero

Mon Oct 30, 2017 9:01 pm

Code: Select all

rec FAIL formats: can't open input  `default': snd_pcm_open error: Permission d$
play FAIL formats: can't open input pipe `|rec --buffer 2048 -d pitch -300': pr$
How can it be permission denied on Pi0 but not on the Pi3 with the same SSD Card? Crazy...

Thoughts?

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

Re: Crontab script works in PI3 but not Zero

Mon Oct 30, 2017 10:49 pm

Is the crontab owner in the audio & video groups?
Are your udev rules the same for both RPi3B and RPIZ?

Have you run sudo apt update && sudo apt -y dist-upgrade && sudo reboot to get firmware/kernel updates?
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.

CapnWinky07
Posts: 12
Joined: Thu Oct 26, 2017 5:39 pm

Re: Crontab script works in PI3 but not Zero

Mon Oct 30, 2017 11:29 pm

Yes, everything is up to date. Not sure about UDEV and the cronowner. I'm pretty new to all of this stuff. This was out of the box noobs R-lite and haven't done anything custom except what was in the article. IS there any easy way to check both?

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Crontab script works in PI3 but not Zero

Tue Oct 31, 2017 12:59 am

Off the top of my head, possibly your program starts running too early in the boot process and tries accessing the USB audio device before the RPi0 has gotten around to / finished detecting and initialising it?
She who travels light — forgot something.

CapnWinky07
Posts: 12
Joined: Thu Oct 26, 2017 5:39 pm

Re: Crontab script works in PI3 but not Zero

Tue Oct 31, 2017 1:16 am

It's possible. Would it be shooting up these errors in that case?

Code: Select all

rec FAIL formats: can't open input  `default': snd_pcm_open error: No such file or directory
play FAIL formats: can't open input pipe `|rec --buffer 2048 -d pitch -300 echos 0.8 0.88 100 0.6 150 .5 band 1.2k 1.5k': premature EOF
That's what's perplexing me.

I do

Code: Select all

Crontab-e
Add this line:

Code: Select all

@reboot sh /home/pi/voicechanger.sh > /home/pi/voicechanger.log 2>&1
My voicechanger.sh simply says:

Code: Select all

play "|rec --buffer 2048 -d pitch -400"
If I just run that in a command prompt, everything works fine. So I don't think it's the mic/speaker/usb card.

CapnWinky07
Posts: 12
Joined: Thu Oct 26, 2017 5:39 pm

Re: Crontab script works in PI3 but not Zero

Tue Oct 31, 2017 1:21 am

For more information:

Lsusb:

Code: Select all

Bus 001 Device 006: ID 0d8c:000c C-Media Electronics, Inc. Audio Adapter
aplay -l:

Code: Select all

card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Set [C-Media USB Headphone Set], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
asound.rc file:

Code: Select all

pcm.!default {
 type hw
 card 1
}

ctl.!default {
 type hw
 card 1
}

User avatar
Paeryn
Posts: 2986
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Crontab script works in PI3 but not Zero

Tue Oct 31, 2017 4:43 am

CapnWinky07 wrote:
Tue Oct 31, 2017 1:16 am
It's possible. Would it be shooting up these errors in that case?

Code: Select all

rec FAIL formats: can't open input  `default': snd_pcm_open error: No such file or directory
play FAIL formats: can't open input pipe `|rec --buffer 2048 -d pitch -300 echos 0.8 0.88 100 0.6 150 .5 band 1.2k 1.5k': premature EOF
That's what's perplexing me.
That's exactly the error you'd get if the requested device doesn't exist.
CapnWinky07 wrote:
Tue Oct 31, 2017 1:16 am
I do

Code: Select all

Crontab-e
Add this line:

Code: Select all

@reboot sh /home/pi/voicechanger.sh > /home/pi/voicechanger.log 2>&1
My voicechanger.sh simply says:

Code: Select all

play "|rec --buffer 2048 -d pitch -400"
If I just run that in a command prompt, everything works fine. So I don't think it's the mic/speaker/usb card.
It works at the command prompt because the audio card will have been detected and set up by the time you get to type anything. Running from cron @reboot you can't guarantee that the services you require have been started before you. Try adding a check to the start of your script and keep waiting until it is seen.

Code: Select all

#!/bin/sh

# If card1 doesn't exist wait for 5 seconds and try again
while [ ! -d /proc/asound/card1 ]; do
    sleep 5
done

# We should have the sound card now so can use it
play "|rec --buffer 2048 -d pitch -400"
Adjust the delay according to your needs, I've given it 5 seconds between checks, don't want to be constantly checking whilst the system is busy booting up.

Although cron isn't really the best place to be doing this, you might be be better off creating a systemd service, that way you should be able to get systemd to only run it after the sound card has appeared if you set its requirements,

Code: Select all

[Unit]
Requires=sys-devices-?????sound-card1.device
After=sys-devices-?????sound-card1.device
To get the actual name (I put ????? for the bit I don't know) run the following, it should list the sound device services that are active (card0 will be the built-in audio, you want the name of card1).

Code: Select all

systemctl status sys-devices-*sound*
She who travels light — forgot something.

pcmanbob
Posts: 9612
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Crontab script works in PI3 but not Zero

Tue Oct 31, 2017 10:12 am

You will probably find that your pi zero is taking longer to boot than your pi3 running on the same SD card.

simple test change your cron line like this.

Code: Select all

 @reboot sleep 60 &&  /home/pi/voicechanger.sh 
it will add a 60 second delay to starting your script and should allow your zero more time to finish booting.
it seems to be a common problem when using stretch that was no present in jessie.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

CapnWinky07
Posts: 12
Joined: Thu Oct 26, 2017 5:39 pm

Re: Crontab script works in PI3 but not Zero

Tue Oct 31, 2017 2:21 pm

Yep! The issue was it not initializing in time! I tried those commands and it started working. It was driving me nuts! Thank you all so much. Mark as resolved!

User avatar
davidcoton
Posts: 5083
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK
Contact: Website

Re: Crontab script works in PI3 but not Zero

Tue Oct 31, 2017 9:43 pm

CapnWinky07 wrote:
Tue Oct 31, 2017 2:21 pm
Mark as resolved!
You do that yourself. Edit your original post and add "[solved]" to the title. 8-)
Signature retired

Return to “Troubleshooting”