Mopidy Autostart

Hello. I installed Mopidy using dietpi-software. I need it to automatically play radio from tunein (or m3u playlist).

I tried:
Editing cron
Custom afterboot dietpi script

Nothing worked…

Thanks

Marekkon5
When exactly the player should start? After user login or automatically on/after boot?

Login: Use /etc/profile.d/custom_script.sh, but custom script option with dietpi-autostart should have the same effect. But as said, you need to manually log in first then.
NB: The script is started with the login user permissions. Thus assure that it does not need e.g. root permissions. As I see, the mopidy service itself is started as root user, thus I guess any access needs root permissions as well (?).

Boot: Add /var/lib/dietpi/postboot.d/custom_script.sh, which will be started after the boot process is fully finished, which is most properly what you want.

If it does not work, check logs /var/log/syslog or journalctl if you find some related error. Also of course run the script manually first to assure it does not contain any syntax error and works as expected.

I had the same wish (start playing a playlist after boot), but just calling mpc (apt-get install mpc) in /var/lib/dietpi/postboot.d/custom_script.sh does not work as mopidy is not started at this point by the boot scripts or at least not ready to accept mpd/mpc communication.

I created a small bash script I like to share. Put this in /var/lib/dietpi/postboot.d/custom_script.sh (as root, chmod ugo+x). Before, create a playlist (e.g.) “default” or “autostart” in mopidy (is a parameter for bash function AutoplayMpc). In short, the script waits for mpd (=mopidy) readiness and then loads the desired playlist, otherwise it times-out after 120s. It forkes to background to not block booting.

#!/bin/bash
# Start mopidy via mpc with given playlist, wait for mpd being ready until timeout
AutoplayMpc () {
        theplaylist=$1
        thetimeout=120 #seconds
        theprefix="[AutostartMpc]“
        logger "$theprefix Started"
        loops=1
        while true;
        do
                mpc >/dev/null 2>&1
                if [ $? -eq 0 ]; then
                        logger "$theprefix MPD found, starting playlist $theplaylist"
                        mpc -q clear >/dev/null
                        mpc -q load $theplaylist >/dev/null
                        mpc -q repeat on >/dev/null
                        mpc -q play  >/dev/null
                        break
                else
                        sleep 1s
                        if [ "$loops" -gt "$thetimeout" ]; then logger "$theprefix MPD timeout" && break; else loops=$((loops+1)); fi
                fi
        done
}
AutoplayMpc "default" & # remove & for blocking execution