Dot3k menu example - code to halt/reboot the Pi?

Hi folks.

I’ve got my Dot3k installed & working. The sample scripts are executing OK. Now I’m trying to modify a copy of the script to include power-off and reboot options. Anyone tried this?

I’ve Googled & found references to including the following in a python script.
import os
import subprocess
from subprocess import call
…and I’m trying the following;
menu.add_item(‘Power off’, ‘call(“sudo halt, Shell=True”)’)
…but that doesn’t appear to work. The Pi doesn’t power down. I’ve tried executing that menu script under sudo but with no difference; it still doesn’t power down. What am I doing wrong?


The second argument to the add_item method should be a function, in this case it looks like you’re passing in a string.

This might work:

menu.add_item('Power off', lambda: call("sudo halt",shell=True))

The lambda keyword produces an un-named function wrapping the call to call() .

Excellent - thank you. Yes, that works. I can now execute sudo halt and sudo reboot using that structure. Just need to run the script at reboot now so the menu is available whenever the system is up.

Tell me… do you know whether it’s possible to run compound operations in that lambda function? I’d like the display to show ‘Powering down’ or some equivalent message when the script calls the halt, or displays ‘Rebooting…’ when it calls a reboot. I speculative appended "& lcd.write(“blah”) in the lambda argument and it didn’t work - but that was just a syntactical guess!

You could place it into a function, and pass the function in place of the lambda, for example:

def my_function():
   call("sudo halt", shell=True)

menu.add_item('Power off', power_off)

You may have trouble drawing anything to the LCD while the menu is running. You’ll need to figure out a way to stop calling “menu.redraw()”.

Thanks :)

I’ll have a play with that once I’ve got the script running automatically. I’m having trouble getting it to start either @reboot or on a ‘every x mins’ basis. It’s weird; the script can be executed from the /advanced folder where it’s stored with …/advanced $ ./ but when I add the reference to crontab (using full path) - either with or without /usr/bin/python before the path (not sure if it’s needed?) - the script still doesn’t appear to be triggered. Well, the dot3k display remains blank anyway.

Can you try triggering the advanced example in crontab @reboot on one of your Pi’s; does it work for you?

My usual setup for triggering a script is to create a startup file that changes into the correct directory and then launches the Python script with sudo. It would look something like this:

cd /home/pi/.../advanced/
sudo python

Or to make it vehemently keep running, I sometimes use this:

cd /home/pi/.../advanced/
while [ 1 ]; do sudo python; sleep 1; done

Then I run this script by adding it to /etc/rc.local

1 Like

Excellent - thank you. Yes, that’s working from boot now :)