Startup Script

Hi Everyone,

I have an mplayer script I want to run once the desktop environment (and any other relent programs) starts up.

Tried to add:
@reboot /root/mplayer_script.sh

At the bottom of /etc/crontab
Sorry still learning my way around bash, loving dietpi btw :smiley:.

Thanks for any help!

Try this using a SystemD service to launch after graphical.target (copy and paste all into terminal):

cat << _EOF_ > /etc/systemd/system/myscript.service
[Unit]
Description=myscript
After=graphical.target

[Service]
Type=oneshot
RemainAfterExit=yes

ExecStart=/bin/bash -c '/root/mplayer_script.sh'

[Install]
WantedBy=default.target
_EOF_
systemctl enable myscript.service
systemctl daemon-reload

reboot, then check the status with:

systemctl status mplayer_script.sh -l

Hey, Fourdee thanks for the assistance! I followed it but ended up with:

root@DietPi:~# systemctl status mplayer_script.sh -l
● mplayer_script.sh.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

Which is strange because if I just copy and paste

root@DietPi:~#/root/mplayer_script.sh

it’ll start playing the video. Could it be root privileges?

Thanks again for your help :slight_smile:

My bad lol :wink:

Correct command is:

systemctl status myscript.service -l

Ah yes that makes sense, interesting results:

● myscript.service - myscript
   Loaded: loaded (/etc/systemd/system/myscript.service; enabled)
   Active: active (exited) since Thu 2016-11-03 05:02:49 GMT; 10min ago
  Process: 1186 ExecStart=/bin/bash -c /root/mplayer_script.sh (code=exited, status=0/SUCCESS)
 Main PID: 1186 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/myscript.service

Nov 03 05:02:47 DietPi systemd[1]: Starting myscript...
Nov 03 05:02:49 DietPi bash[1186]: Cannot find HOME directory.
Nov 03 05:02:49 DietPi bash[1186]: MPlayer2 2.0-728-g2c378c7-4 (C) 2000-2012 MPlayer Team
Nov 03 05:02:49 DietPi bash[1186]: Terminal type `unknown' is not defined.
Nov 03 05:02:49 DietPi bash[1186]: Playing big_buck_bunny_1080p_H264_AAC_25fps_7200K.MP4.
Nov 03 05:02:49 DietPi bash[1186]: Cannot open file 'big_buck_bunny_1080p_H264_AAC_25fps_7200K.MP4': No such file or directory
Nov 03 05:02:49 DietPi bash[1186]: Failed to open big_buck_bunny_1080p_H264_AAC_25fps_7200K.MP4.
Nov 03 05:02:49 DietPi bash[1186]: Dead key input on file descriptor 0
Nov 03 05:02:49 DietPi bash[1186]: Exiting... (End of file)
Nov 03 05:02:49 DietPi systemd[1]: Started myscript.

So it does run the script, but when its run it can’t see root directory or any contents. Is it because myscript.service is being run outside of root access?

Can you paste the contents of your script? I’ll take a look.

#!/bin/sh
set -e

if [ ! -w /dev/cedar_dev ]; then
        # chmod 666 /dev/cedar_dev
        echo "Error: /dev/cedar_dev missing or no access"
        exit 1
fi

if [ ! -w /dev/ion ]; then
        # chmod 666 /dev/ion
        echo "Error: /dev/ion missing or no access"
        exit 1
fi

if [ ! -w /dev/disp ]; then
        # chmod 666 /dev/disp
        echo "Error: /dev/disp missing or no access"
        exit 1
fi

export VDPAU_DRIVER=sunxi
DISPLAY=:0 mplayer -fs -autosync 50 -mc 1.5 -vo vdpau -vc ffmpeg12vdpau,ffh264vdpau,ffhevcvdpau, big_buck_bunny_1080p_H264_AAC_25fps_7200K.MP4 -loop 0

Try defining the absolute filepath to big_buck_bunny_1080p_H264_AAC_25fps_7200K.MP4. eg:

/root/mystuff/big_buck_bunny_1080p_H264_AAC_25fps_7200K.MP4

Hey Fourdee,

Sorry for the late reply, went through exams and my image was corrupted so I went back and remade it.

Some of the names have changed but much the same as before.
On the plus side when I rebooted the audio was playing but the video didn’t show, so that’s positive step forward :slight_smile: .

This is the output from the script.

Thanks again!

root@DietPi:~# systemctl status myscript.service -l
● myscript.service - myscript
   Loaded: loaded (/etc/systemd/system/myscript.service; enabled)
   Active: activating (start) since Wed 2016-11-30 00:17:38 GMT; 2min 27s ago
 Main PID: 908 (mplayer-play.sh)
   CGroup: /system.slice/myscript.service
           ├─908 /bin/sh /EBTV/mplayer-play.sh
           └─911 mplayer -fs -autosync 50 -mc 1.5 -vo vdpau -vc ffmpeg12vdpau,ff                                                                                                                                                             h264vdpau,ffhevcvdpau, /EBTV/big_buck_bunny_1080p_H264_AAC_25fps_7200K.MP4 -loop                                                                                                                                                              0

Nov 30 00:19:10 DietPi bash[908]: [2.0K blob data]
Nov 30 00:19:16 DietPi bash[908]: [1.9K blob data]
Nov 30 00:19:23 DietPi bash[908]: [1.9K blob data]
Nov 30 00:19:28 DietPi bash[908]: [1.9K blob data]
Nov 30 00:19:33 DietPi bash[908]: [1.9K blob data]
Nov 30 00:19:39 DietPi bash[908]: [1.9K blob data]
Nov 30 00:19:46 DietPi bash[908]: [1.9K blob data]
Nov 30 00:19:52 DietPi bash[908]: [1.9K blob data]
Nov 30 00:19:58 DietPi bash[908]: [1.9K blob data]
Nov 30 00:20:04 DietPi bash[908]: [1.9K blob data]

Mplayer should work on fbdev, so not sure why there is no video. Does the video work when you run from the cmdline? Might also be SystemD, try adding StandardOutput=tty under the [Service] section of the service.

Failing that, lets try X11, change your script so that xinit is called, before mplayer, eg:

xinit mplayer -fs -autosyn......................

Also, make sure you have X11 installed:

dietpi-software install 6

I gave that a shot and ended up with some fatal errors in the log lol (x11 was installed).

But that’s all good, when I watched the boot I noticed the audio would begin to play before the boot messages would finish and the desktop would eventually load. I decided to add a sleep 60 to the beginning of my script and when it rolled around it all loaded up correctly.

Its hacky as all hell and the OCD part of me would like something more concrete but I am happy that it loads :slight_smile:, thank you for your help Fourdee, really do appreciate it.

Not sure if there is a way to tell myscript.services to wait until x11 has loaded or potentially some other processes so it runs once all other processes are operational?

after I get this baby operational I’ll be moving on to playing with cloud stuff :slight_smile:

:smiley:

Not sure if there is a way to tell myscript.services to wait until x11 has loaded or potentially some other processes so it runs once all other processes are operational?

Yep, is your systemD service currently set to After=graphical.target?

cat << _EOF_ > /etc/systemd/system/myscript.service
[Unit]
Description=myscript
After=graphical.target

[Service]
Type=oneshot
RemainAfterExit=yes

ExecStart=/bin/bash -c '/root/mplayer_script.sh'

[Install]
WantedBy=default.target
_EOF_
systemctl enable myscript.service
systemctl daemon-reload

If After=graphical.target isnt waiting for X11, you can add some code to your script that will wait for the X11 process to start, eg (untested):

# - Wait for X11 process to run
while (( ! $(ps aux | grep -ci -m1 '[X]11') ))
do 
	# - wait 3 seconds before checking again
	sleep 3
	
done

play movie

Yep, is your systemD service currently set to > After=graphical.target> ?

cat << EOF > /etc/systemd/system/myscript.service
[Unit]
Description=myscript
After=graphical.target

[Service]
Type=oneshot
RemainAfterExit=yes

ExecStart=/bin/bash -c ‘/root/mplayer_script.sh’

[Install]
WantedBy=default.target
EOF
systemctl enable myscript.service
systemctl daemon-reload

>
> If > After=graphical.target > isnt waiting for X11, you can add some code to your script that will wait for the X11 process to start, eg (untested):
>
> ```text
# - Wait for X11 process to run
while (( ! $(ps aux | grep -ci -m1 '[X]11') ))
do 
	# - wait 3 seconds before checking again
	sleep 3
	
done

play movie

Yep After=graphical.target is set in myscript.service, it doesn’t appear to be waiting.
Had to modify your script slightly to ( !(ps aux | grep -ci -m1 ‘11’) ) it works! :smiley:
Didn’t see the desktop before the video started to play, feels good hehe.

Thanks again!