Starting Mycroft AI at boot time

Hello mycrofters,

I wrote in another topic it would be nice to have mycroft running at boot/reboot time. As such, I wrote a script /etc/init.d/mycroftd:

cd /etc/init.d
cat mycroftd
#!/bin/bash
### BEGIN INIT INFO
# Provides:          raspi-config
# Required-Start: $local_fs $syslog $remote_fs dbus bluetooth
# Required-Stop:
# Default-Start: S 2 3 4 5
# Default-Stop:
# Short-Description: Start/stop/restart Mycroft AI
# Description:
### END INIT INFO

. /lib/lsb/init-functions

function startMycroft {
  # runuser -s /bin/bash pi -c "home/pi/mycroft-core/bin/mycroft-start all" # start all mycroft processes as pi
  home/pi/mycroft-core/bin/mycroft-start all    # start all mycroft processes as pi
  rc=$?
  if [ "$rc" != 0 ]; then                       # error
    echo "ERROR: /home/pi/mycroft-core/bin/mycroft-start all returned $rc"
    exit 1
  fi
 }

function stopMycroft {
  # runuser -s /bin/bash pi -c "/home/pi/mycroft-core/bin/mycroft-stop all" # stop all mycroft processes
  /home/pi/mycroft-core/bin/mycroft-stop all    # stop all mycroft processes
  rc=$?
  if [ "$rc" != 0 ]; then                       # error
    echo "ERROR: /home/pi/mycroft-core/bin/mycroft-stop all returned $rc"
    exit 2
  fi
 }

# main() - perform start, stop, restart or info actions
case "$1" in
  start)
    startMycroft
    ;;
  stop)
    stopMycroft
    ;;
  restart)                                      # stop then start mycroft processes
    stopMycroft
    startMycroft
    ;;
  info)
    echo "mycroft processes running:"
    ps -ef | grep mycroft | grep -v grep
    ;;
  *)
    echo "Usage: $0 start|stop|restart|info|status" >&2
    exit 3
    ;;
esac

I then made a symlink to run level 3:

cd etc/rc3.d
ln -s ../init.d/mycroftd S01mycroftd
ls -l S01mycroftd
lrwxrwxrwx 1 root root 18 Jan  4 11:54 /etc/rc3.d/S01mycroftd -> ../init.d/mycroftd

Now when I boot/reboot my Pi 4, mycroft is running:

service mycroftd status
● mycroftd.service - LSB: Start/stop/restart Mycroft AI
   Loaded: loaded (/etc/init.d/mycroftd; generated)
   Active: active (running) since Sat 2020-01-04 12:10:38 EST; 1min 2s ago
...
service mycroftd info
mycroft processes running:
root       553     1 11 12:10 ?        00:00:09 python3 -m mycroft.messagebus.service
root       556     1 18 12:10 ?        00:00:14 python3 -m mycroft.skills
root       559     1  7 12:10 ?        00:00:05 python3 -m mycroft.audio
root       563     1 10 12:10 ?        00:00:08 python3 -m mycroft.client.speech
root       566     1  4 12:10 ?        00:00:03 python3 -m mycroft.client.enclosure
...

Hope this helps. Any feedback is welcome (for example, not sure about the runuser lines commented out. Should mycroft be running as pi and not root?). Maybe it could be integrated into picroft.

-Mike Mac

P.S. I probably should be using the newfangled systemd, but it’s good to see a hybrid of good old-fashioned SysVinit and systemd.

2 Likes

Great work. I did not know if SysVinit still worked so I spent the past few days getting Mycroft working at boot via systemd: