Thursday, March 17, 2022

[SOLVED] I want to run a bunch of terminal commands consecutively, and repeatedly, on a raspberry pi

Issue

I have a raspberry pi 4 with a inkyWHAT display, I've managed to get the display showing my own images.

What I need help with is to run the following commands one after the other, at present I paste each line in one at a time:

from PIL import Image
from inky import InkyWHAT
inky_display = InkyWHAT("yellow")
inky_display.set_border(inky_display.YELLOW)
img = Image.open("/home/pi/Desktop/test2.jpg")
w, h = img.size
h_new = 300
w_new = int((float(w) / h) * h_new)
w_cropped = 400
img = img.resize((w_new, h_new), resample=Image.LANCZOS)
x0 = (w_new - w_cropped) / 2
x1 = x0 + w_cropped
y0 = 0
y1 = h_new
img = img.crop((x0, y0, x1, y1))
pal_img = Image.new("P", (1, 1))
pal_img.putpalette((255, 255, 255, 0, 0, 0, 255, 255, 0) + (0, 0, 0) * 252)
img = img.convert("RGB").quantize(palette=pal_img)
inky_display.set_image(img)
inky_display.show()

Not only that, but I'd like to run this every 15 minutes, or so. With no interaction from me at all.

I appreciate this may be really basic for some but this will be my very first time venturing into this kind of thing.


Solution

Just save the file as myfile.py then in a terminal issue

chmod +x myfile.py  # to make file executable ( only need to do this once )

python myfile.py  #  execute the code

By the looks of it it wants to create an output window ... if its output is not visual namely is some output file then you can put into a cronjob to run at some frequency ... issue crontab -l to display local cronjobs on a given machine for current user ... get into edit mode to edit cronjobs by issuing crontab -e ... here is a one liner to run something every 15 minutes

0,15,30,45 * * * *  /usr/bin/python /some/dir/myfile.py

cronjob is created per user and it does not source your ~/.bashrc so if it needs jacked up env vars then those must be made visible to a given cronjob sometime its handy to create a wrapper bash shell script to do those preliminary and post processing steps YMMV



Answered By - Scott Stensland
Answer Checked By - David Marino (WPSolving Volunteer)