Failed to restart dietpi-autostart custom.service: Unit dietpi-boot.service Not found

I’m using the newest dietpi version on a RPi3b and have selected the autostart custom script (background with no script) and typed in a python script to be run at boot. python3 /root/yahboom-hat/rgb-cooling-hat-display.py
And

python3 /root/yahboom-hat/rgb-cooling-hat-display.py

I don’t know if I’m doing this wrong or if the path is wrong. For some reason only the display script starts but not the fan script. I’m not sure where alse I put the display script but that works. However when I enter systemctl restart dietpi-autostar t_ custom
It results in this.


Failed to restart dietpi-autostart custom.service:
Unit dietpi-boot.service not found.

I dunno what’s happening can some give me some ideas or a soloution?

NB: the fan hat is the yahboom display fan hat.

What happens when you run the script by itself (without the service)?

Can you post the script into the thread so we can take a look?

1 Like

Sure. Also to answer your questions the script runs. But after I cd into the directorty does it run. Not on root user.

Code for the fan and I think for the RGB

import smbus
import time
import os
import sys
from datetime import datetime
from array import *

def setfanspeed(oVal):
    try:
        bus.write_byte_data(addr, fan_reg, oVal)
    except:
        exc_tuple = sys.exc_info()
        print("setfanspeed [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])

def setRGBEffect(effect):
    try:
        if effect >= 0 and effect <= 4:
            bus.write_byte_data(addr, rgb_effect_reg, effect&0xff)
    except:
        exc_tuple = sys.exc_info()
        print("setRGBEffect [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])

def setRGBSpeed(speed):
    try:
        if speed >= 1 and speed <= 3:
            bus.write_byte_data(addr, rgb_speed_reg, speed&0xff)
    except:
        exc_tuple = sys.exc_info()
        print("setRGBSpeed [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])

def setRGBColor(color):
    try:
        if color >= 0 and color <= 6:
            bus.write_byte_data(addr, rgb_color_reg, color&0xff)
    except:
        exc_tuple = sys.exc_info()
        print("setRGBColor [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])

def setrgb(num,r,g,b):
    try:
        #turn lights off first
        #bus.write_byte_data(addr, rgb_off_reg, 0x00)
        if num >= max_led:
            bus.write_byte_data(addr,0x00,0xff)
            bus.write_byte_data(addr,0x01,r&0xff)
            bus.write_byte_data(addr,0x02,g&0xff)
            bus.write_byte_data(addr,0x03,b&0xff)
        elif num >= 0:
            bus.write_byte_data(addr,0x00,num&0xff)
            bus.write_byte_data(addr,0x01,r&0xff)
            bus.write_byte_data(addr,0x02,g&0xff)
            bus.write_byte_data(addr,0x03,b&0xff)
    except:
        exc_tuple = sys.exc_info()
        print("setrgb [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])


bus = smbus.SMBus(1)
addr = 0x0d
fan_reg = 0x08
state = 0
temp = 0
level_temp = 0
rgb_effect_reg = 0x04
rgb_speed_reg = 0x05
rgb_color_reg = 0x06
rgb_off_reg = 0x07
max_led = 3
rgb = [0x00, 0x00, 0x00, 0x00]

bus.write_byte_data(addr, rgb_off_reg, 0x00)
time.sleep(1)
setRGBEffect(1)
setRGBSpeed(3)
setRGBColor(4)


while True:
    cmd = os.popen('vcgencmd measure_temp').readline()
    CPU_TEMP = cmd.replace("temp=","").replace("'C\n","")
    temp = float(CPU_TEMP)

    if abs(temp - level_temp) >= 1:
        if temp <= 40:
            level_temp = 40
            fan_speed = 0x7
            rgb = [max_led, 0x00, 0x00, 0xff]
        elif temp <= 45:
            level_temp = 0x7
            fan_speed = 0x7
            rgb = [max_led, 0x1e, 0x90, 0xff]
        elif temp <= 47:
            level_temp = 47
            fan_speed = 0x08
            rgb = [max_led, 0x00, 0xbf, 0xff]
        elif temp <= 49:
            level_temp = 49
            fan_speed = 0x06
            rgb = [max_led, 0x5f, 0x9e, 0xa0]
        elif temp <= 51:
            level_temp = 51
            fan_speed = 0x09
            rgb = [max_led, 0xff, 0xff, 0x00]
        elif temp > 51:
            level_temp = 52
            fan_speed = 0x01
            rgb = [max_led, 0xff, 0x00, 0x00]
        else:
            level_temp = 0
            fan_speed = 0x01
            rgb = [max_led, 0xff, 0xff, 0xff]
            print('Something wrong: choosing fan reg 0x01 instead')
    setfanspeed(fan_speed)
    print(CPU_TEMP + 'c: <' + str(level_temp) + ' : choosing fan reg ' + str(hex(fan_speed)))
    setrgb(rgb[0],rgb[1],rgb[2],rgb[3])
    sys.stdout.flush()
    time.sleep(4)

and the code for the display

import Adafruit_GPIO.I2C as I2C

import time
import os
import smbus
import sys
bus = smbus.SMBus(1)

import Adafruit_SSD1306

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

import subprocess

hat_addr = 0x0d
rgb_effect_reg = 0x04
fan_reg = 0x08
fan_state = 2
count = 0

# Raspberry Pi pin configuration:
RST = None     # on the PiOLED this pin isnt used

# 128x32 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)

# Initialize library.
disp.begin()

# Clear display.
disp.clear()
disp.display()

# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))

# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)

# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)

# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0

# Load default font.
font = ImageFont.load_default()

# Alternatively load a TTF font.  Make sure the .ttf font file is in the same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
#font = ImageFont.truetype('/opt/rgb-cooling-hat/nk57.ttf', 8)

def setFanSpeed(speed):
    bus.write_byte_data(hat_addr, fan_reg, speed&0xff)

def setRGBEffect(effect):
    bus.write_byte_data(hat_addr, rgb_effect_reg, effect&0xff)

def getCPULoadRate():
    f1 = os.popen("cat /proc/stat", 'r')
    stat1 = f1.readline()
    count = 10
    data_1 = []
    for i  in range (count):
        data_1.append(int(stat1.split(' ')[i+2]))
    total_1 = data_1[0]+data_1[1]+data_1[2]+data_1[3]+data_1[4]+data_1[5]+data_1[6]+data_1[7]+data_1[8]+data_1[9]
    idle_1 = data_1[3]

    time.sleep(1)

    f2 = os.popen("cat /proc/stat", 'r')
    stat2 = f2.readline()
    data_2 = []
    for i  in range (count):
        data_2.append(int(stat2.split(' ')[i+2]))
    total_2 = data_2[0]+data_2[1]+data_2[2]+data_2[3]+data_2[4]+data_2[5]+data_2[6]+data_2[7]+data_2[8]+data_2[9]
    idle_2 = data_2[3]

    total = int(total_2-total_1)
    idle = int(idle_2-idle_1)
    usage = int(total-idle)
    print("idle:"+str(idle)+"  total:"+str(total))
    usageRate =int(float(usage * 100/ total))
    print("usageRate:%d"%usageRate)
    return "CPU:"+str(usageRate)+"%"


def setOLEDshow():
    # Draw a black filled box to clear the image.
    draw.rectangle((0,0,width,height), outline=0, fill=0)

    #cmd = "top -bn1 | grep load | awk '{printf \"CPU:%.0f%%\", $(NF-2)*100}'"
    #CPU = subprocess.check_output(cmd, shell = True)
    CPU = getCPULoadRate()

    cmd = os.popen('vcgencmd measure_temp').readline()
    CPU_TEMP = cmd.replace("temp=","Temp:").replace("'C\n","C")
    global g_temp
    g_temp = float(cmd.replace("temp=","").replace("'C\n",""))

    cmd = "free -m | awk 'NR==2{printf \"RAM:%s/%s MB \", $2-$3,$2}'"
    MemUsage = subprocess.check_output(cmd, shell = True)

    cmd = "df -h | awk '$NF==\"/\"{printf \"Disk:%d/%dMB\", ($2-$3)*1024,$2*1024}'"
    Disk = subprocess.check_output(cmd, shell = True)

    cmd = "hostname -I | awk '{ print $1 }'"
    IP = subprocess.check_output(cmd, shell = True)

    # Write two lines of text.

    draw.text((x, top), str(CPU), font=font, fill=255)
    draw.text((x+56, top), str(CPU_TEMP), font=font, fill=255)
    draw.text((x, top+8), MemUsage.decode('utf-8'),  font=font, fill=255)
    draw.text((x, top+16), Disk.decode('utf-8'),  font=font, fill=255)
    draw.text((x, top+24), "wlan0:" + IP.decode('utf-8'),  font=font, fill=255)

    # Display image.
    try:
        disp.image(image)
        disp.display()
        time.sleep(.1)
    except:
        exc_tuple = sys.exc_info()
        print("setoledshow [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])

#setFanSpeed(0x00)
#setRGBEffect(0x03)

while True:
    setOLEDshow()	
#    if g_temp >= 55:
#        if fan_state != 1:
#            setFanSpeed(0x01)
#            fan_state = 1        
#    elif g_temp <= 48:
#        if fan_state != 0:
#            setFanSpeed(0x00)
#            fan_state = 0
#    
#    if count == 10:
#        setRGBEffect(0x04)
#    elif count == 20:
#        setRGBEffect(0x02)
#    elif count == 30:
#        setRGBEffect(0x01)
#    elif count == 40:
#        setRGBEffect(0x03)
#        count = 0
#    count += 1
# was time.sleep(.5)

    sys.stdout.flush()
    time.sleep(5)

Try to use an absolute path like

/usr/bin/python3 /root/yahboom-hat/rgb-cooling-hat-display.py

After boot you can also use journalctl -u dietpi-autostart_custom at any time to see the script output, if there is any.

1 Like

Okay, cool, ill try. But is there a reason why it says failed to restart diet pi autostart?

I think it is not an actual service. As I understand it, the custom script is just executed once after boot.
So you could test this by just running the script on your running system and if this works it should also work after reboot.

1 Like

Well I’ve tried to put full path restart and what not and it doesn’t work. But when I do it in CL it’s works but paste the temps or the current dataset or whatever. And I can’t use it after that and need to restart it. Back to the topic I think the script runs because of RC.local. or the script that runs at boot on vanilla raspbian is. Not sure thou… Appreciate the help thou mate!

What happens when you try to run the autostart script on the running system?
(you have make the script executable, I think this permission is removed after the reboot for reasons)

sudo chmod +x /var/lib/dietpi/dietpi-autostart/custom.sh
/var/lib/dietpi/dietpi-autostart/custom.sh
sudo chmod -x /var/lib/dietpi/dietpi-autostart/custom.sh
1 Like

@MichaIng can you have a look.

1 Like

I’ll do so when I’m back home. I appreciate the help! Sorry to bother you.

I think you don’t bother anyone, this forum is for problems like yours :slight_smile:

2 Likes

For some reason, it only displays output from the display script. Not the fan script.

Can you show us your autostart script?

cat /var/lib/dietpi/dietpi-autostart/custom.sh
1 Like

Good news, I added and andersand (this &) and it worked! I also added sudo at the start.

1 Like

also here

root@SotariaPi:~# cat /var/lib/dietpi/dietpi-autostart/custom.sh
#!/bin/bash
# DietPi-Autostart custom script
# Location: /var/lib/dietpi/dietpi-autostart/custom.sh


sudo /usr/bin/python3 /root/yahboom-hat/rgb-cooling-hat-display.py &
sudo /usr/bin/python3 /root/yahboom-hat/rgb-cooling-hat-fan.py &

exit 0
root@SotariaPi:~#


@Jappe thanks a lot for your help! I appreciate it very much mate!

yes, this will send the script into background and continue with the next one.

1 Like

The other error however is strange. systemctl status dietpi-boot does exist, right?