[SOLVED] How to delay MyCroft services startup?

Hi there,

I’m currently running the picroft enclosure/image, on a Raspberry Pi 3B+, using a Seeed studio 4-mic array hat.

Everything is working fairly well (early stages at the moment) except when rebooting the RPi.

After a reboot or after removing/reconnecting power, MyCroft will not wake up, even several minutes later after the boot. Connecting over SSH and running the mycroft-cli-client shows that the microphone is providing no input, as the mic level indicator does nothing.

Performing “mycroft-stop” followed by “mycroft-start all” fixes the issue. Mic indicator shows input, wake word and commands all work as expected.

I’m putting this down to services starting out of order during boot. I’m guessing the mycroft services are starting before the microphone is fully ready.

My question is how can I delay the startup of mycroft services following a system boot of the RPi? I’d like to add an extra ~30 second delay as a starting point

Is it some kind of systemd config? Or is there a scheduled task that calls a startup script?

I am a Microsoft refugee who has been slowly getting into more and more Linux/FOSS over the last 6 months, so please don’t be afraid to explain it like I’m five.

Thanks for your time.

- Michael

What’s in the logs (/var/log/mycroft)?

Relevant log info from voice.log at the end of this post.

I can confirm it’s only the voice service that needs to be restarted.

“mycroft-start voice restart” solves the issue just as well instead of
“mycroft-stop”
“mycroft-start all”

I’m not 100% but I think all of this log output was from after the restart. The line before this was timestamped yesterday, before I unplugged the RPi for a period.

2020-02-18 12:12:07.910 | INFO     |  2117 | mycroft.messagebus.load_config:load_message_bus_config:33 | Loading message bus configs
2020-02-18 12:12:11.354 | INFO     |  2117 | mycroft.util:find_input_device:250 | Searching for input device: seeed-4mic-voicecard
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c$
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c$
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c$
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c$
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c$
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port
ALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playback
ALSA lib conf.c:5014:(snd_config_expand) Unknown parameters {AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2  CARD 0}
ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM iec958:{AES0 0x6 AES1 0x82 AES2 0x0 AES3 0x2  CARD 0}
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card
Expression 'alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )' failed in 'src/hostapi/alsa/pa_linux_alsa.c$
2020-02-18 12:12:11.708 | INFO     |  2117 | mycroft.client.speech.listener:create_wake_word_recognizer:323 | Creating wake word engine
2020-02-18 12:12:11.711 | INFO     |  2117 | mycroft.client.speech.listener:create_wake_word_recognizer:346 | Using hotword entry for hey mycr$
2020-02-18 12:12:11.717 | INFO     |  2117 | mycroft.client.speech.hotword_factory:load_module:386 | Loading "hey mycroft" wake word via preci$
2020-02-18 12:12:17.106 | INFO     |  2117 | mycroft.client.speech.listener:create_wakeup_recognizer:360 | creating stand up word engine
2020-02-18 12:12:17.109 | INFO     |  2117 | mycroft.client.speech.hotword_factory:load_module:386 | Loading "wake up" wake word via pocketsph$
2020-02-18 12:12:17.717 | INFO     |  2117 | mycroft.messagebus.client.client:on_open:67 | Connected

systemctl | grep mycroft

Returns nothing, so, it’s not a service at all? How is mycroft started at boot?

Best I can see is some entries in htop that indicate python is the owning process. But I understand next to nothing of python so can’t begin to figure it out.

I you are on picroft, then you could add

sleep 5s

into /home/pi/custom_setup.sh to have it waite 5 seconds before continue.
That file is executed before mycroft services are started from auto_run.sh

picroft isnt starting mycroft services from systemd…it is running auto_run.sh on each boot. You can verify that services are running by

ps ax | grep " -m mycroft." 

then you see something like this:

pi@picroft1:~ $ ps ax |grep " -m mycroft."
 3508 pts/1    Sl   141:07 python3 -m mycroft.messagebus.service
 3511 pts/1    Sl   343:24 python3 -m mycroft.skills
 3514 pts/1    Sl    19:07 python3 -m mycroft.audio
 3517 pts/1    Sl   112:15 python3 -m mycroft.client.speech
 3520 pts/1    Sl    23:57 python3 -m mycroft.client.enclosure
 8196 pts/2    Sl+  923:45 python -m mycroft.client.text
31589 pts/5    Sl+    3:57 python -m mycroft.client.text
31940 pts/1    R+     0:00 grep --color=auto  -m mycroft.
pi@picroft1:~ $ 

OK thank you, that’s seeming a lot clearer.

  • So auto_run.sh is called from the ~/.bashrc file, which is executed on every login.
  • Auto login (resulting in execution of bashrc) can be enabled using sudo raspi-config and selecting 3 -> Boot Options.
  • On lines 897-901 of auto_run.sh, it checks for the existence of custom_setup.sh, and if it exists, executes it.
  • After that, on line 907 of auto_run.sh, the actual mycroft services are started.

So the sleep entry still didn’t work.

Here’s what I did to get the problem fixed:

  • Commented out the auto_run.sh line at the end of ~/.bashrc

Then, following instructions from the Respeaker Core V2 thread by @Dominik (link at the end):

  • Created the file /etc/systemd/system/mycroft.service with the following content:

    [Unit]
    Description=Mycroft personal AI
    After=seeed-voicecard.service

    [Service]
    User=pi
    WorkingDirectory=/home/pi/mycroft-core
    ExecStart=/home/pi/mycroft-core/start-mycroft.sh all
    ExecStop=/home/pi/mycroft-core/stop-mycroft.sh
    Type=forking
    Restart=always
    RestartSec=3

    [Install]
    WantedBy=multi-user.target

  • Added the pi user the the pulse and pulse-access groups, not sure whether this was necessary.

  • Enabled the service sudo systemctl enable mycroft.service

Microphone now seems to start reliably after multiple reboots. And connecting over ssh is faster as it isn’t running auto_run.sh every time.

1 Like