[Guide] Quick Restart on OLED Service

I created a simple project for those of you who are interested. I’m currently working on the oled services and need to constantly stop and start the service. For the guys out there also tinkering with this, I’d recommend following the following following post by @CAProjects on enabling a project from the settings menu.

After you have followed this tutorial on enabling the projects menu, navigate to /home/user and create yourself an oled projects folder if you haven’t already done so. Create a sub directory here and call it “Restart OLED” This will be the name that is displayed in the projects menu.

Once the folder has been created, we need to make a minimum of 1 file, but the way the projects folder works, we need 2 for completeness. Create the new files and call them:

start.sh
stop.sh

Now, open start.sh in a text editor of your choice and paste the following:

sudo systemctl restart pt-sys-oled.service

Save and close the text editor. No open a terminal in this folder and perform the following commands:

chmod u+x start.sh
chmod u+x stop.sh

This gives the scripts executable privileges.

You should be ready now. On the pi-top, press the circle button and get yourself to where it is showing “Settings” on the screen. Click the down button to navigate to projects. Press the circile button again on “Projects” You should be able to scroll through the list and find “Restart OLED.” Once you find it, press the circle button one more time and you should be able to see that the OLED goes blank after a few seconds and then comes back (granted you didn’t change anything and there are no errors.)

If it fails to come back, be sure to check out the system daemon log @ /var/log/daemon.log to find out what happened when the service was attempting to restart.

Final note: if you don’t see “Restart OLED” in the projects folder, try backing out and then re-entering the projects folder. I found that sometimes when making a new folder, even though it is supposed to update right away, it doesn’t always. This forces it to refresh.


How this works:

The projects menu will start executing its own calling code the moment the user requests to run a particular project. This code checks to see if there are any scripts in your project directory currently running and if there is, will try to run the stop.sh script from your project folder. If there is no stop.sh script in your folder, then it will attempt to kill the process by finding its process id.

Once this is done, it will then start to execute start.sh. If your bash script blocks indefinitely, then use the stop.sh to clean things up. If you don’t use the stop.sh script, then it will kill the process mid instruction regardless and can leave your project in an unknown state, like when reading or writing files, running a python script, etc etc. But if your project will terminate the moment the instruction is complete, there really is no need to use stop.h If an instruction ever hangs or a program hangs after being called from the start.sh, chances are you’re going to want to terminate that process anyways.

Enjoy!

4 Likes

I have no idea why but no script gets executed, I have got the start and stop sh files and have chmod them, can run them in terminal but does not run via pressing the O button

something interesting tho, can have python files named main.py instead of start.sh

Mar 12 13:05:33 pi-top pt-device-manager[452]: [13:05:33.968753 I] Publishing message: PUB_V3_BUTTON_SELECT_PRESSED
Mar 12 13:05:34 pi-top pt-sys-oled[3342]: [13:05:34.106485 I] Starting project: /home/pi/OLEDProjects/RestartOLED/main.py
Mar 12 13:05:34 pi-top pt-sys-oled[3342]: [13:05:34.106852 I] No code file found at /home/pi/OLEDProjects/RestartOLED/main.py/start.sh
Mar 12 13:05:34 pi-top pt-device-manager[452]: [13:05:34.204345 I] Publishing message: PUB_V3_BUTTON_SELECT_RELEASED

Looks like something odd is happening with my install, regardless what i do it still kas my test_code.py display even though the file has been deleted, looks like another reinstall for me :frowning:

A fresh install did not fix the issue BUT I did find a solution

@Supernovali, Might want to make a little edit

#!/bin/sh
sudo systemctl restart pt-sys-oled.service 

It requires a shebang line for it to execute for me

Also i done a system restart one with the simple

#!/bin/sh
sudo reboot 

however i am going to maybe switch it to python and provide a counter to be able to cancel the reboot

@Supernovali

An here is a working reboot script that you can cancel the reboot

start.sh - the path to main.py can be anywhere and can be called anything to be honest

#!/bin/sh
sudo /home/pi/OledProjects/test/main.py

main.py

#!/usr/bin/env python3

from time import sleep
from os import system
from pitop.miniscreen import Miniscreen
from PIL import Image, ImageDraw, ImageFont

ms = Miniscreen()
image = Image.new(ms.mode, ms.size,)
canvas = ImageDraw.Draw(image)
ms.set_max_fps(60)

up = ms.up_button
down = ms.down_button
select = ms.select_button
cancel = ms.cancel_button

def terminate():
    canvas.rectangle(ms.bounding_box, fill=0)
    canvas.text((0, 0),f"Cancelling Reboot",font=ImageFont.load_default(),fill=1)
    ms.display_image(image)
    sleep(2)
    exit()

for x in range(10):
    if cancel.is_pressed:
        select.when_pressed = terminate
        
    else:
        canvas.rectangle(ms.bounding_box, fill=0)
        canvas.text((0, 0),f"Reboot in: {10-x}",font=ImageFont.load_default(),fill=1)
        canvas.text((0, 23),f"Hold X button",font=ImageFont.load_default(),fill=1)
        canvas.text((0, 36),f"to STOP reboot",font=ImageFont.load_default(),fill=1)
        ms.display_image(image)
        sleep(1)
        if x == 9:
            system('sudo reboot')
3 Likes

Haha love it! I just woke up haha. I was wiped! I just woke up. I’m going to have to give this a go in a little bit. I have to complete the prototype display adapter and test it. I ended up making a few mistakes on it so I’m going try a couple things but I need to make sure the concept works first. :slight_smile:

@CAProjects: Thanks for sharing this. I tested it out and works very nice. Like the idea you posted in another post (can’t recall exactly which) but it had some sort of pages. Would be nice to have something la a process killer/task manager menu😁
| Up. Task/process. Kill|
| Down. Exit|

Or a mini menu for python apps from a predefined list.
| Up. Example.py. Start|
| Down. Stop|

Hope this makes sense😂

1 Like

It does. We’ve been working on getting the projects folder to show a list of scripts that can be run by oled :slight_smile: the current way it works is that if the script is already running, it tries to stop it. I’m driving into hardware right now but @CAProjects may have more time to look into this

1 Like

Well 1 thing is for sure, its impossible to have that as a widget. as a project yes but that would take quite a while and not something that i would be interested in taking on

Again, not as a widget, but as a project yes. personally i am not sure on how to handle tasks in python right now and i have a project in the works if my parts actually get delivered.

The way i would do this would be up and down to navigate the processes and use select to O to start X to kill but again, i really know nothing about task management in python at this time

@Luis pages thing, that was just a testing of the mini screen buttons, possibly not going to use that at all, was a time filler just to learn something

1 Like

Redefine your scripts as a service and use system() to obtain a list of services that match a “script” list in the projects menu. This allows you to continue using the model of a start and stop script, or even make each start/stop script responsible for reporting it’s status to the “project” item and handle its own service

1 Like

Yeah, it’s way out my current scope of work right now so it’s not something I will be looking into sadly

2 Likes

Thanks guys for the input. I will just go with the plain old shortcuts on the touchscreen display.

I played around with the restart script @CAProjects created and modified this so I can start my dslr camera control service but integrating this in my menu system didn’t work so great. My menu system is not stock as I have played around with projects. Think I will need to start from scratch at one point and follow the guide @CAProjects shared.

Have a great weekend,
Luis

If you want info displayed on the screen then a widget is fine, if you want to control something or start something or Thrace total control of the screen and buttons then a project is better

1 Like

So there isn’t really any support for taking over the mini screen through a project launched there? Gotcha. I’ll have to see what I can work up as a widget then