Hi everyone
New here using python. I have a PI hooked up to a RGB led matrix, and an Adafruit RGB Matrix HAT that has the clock built in.
I am running a couple of text scripts manually by just typing the sudo ./runtext.py or runtext1.py or runtext2.py etc. that runs a different text and I run the script at certain times in the day. This is because I use the display as a sign IE: Open, closed etc.
Is there a simple python script I could insert that would run script 1 say at 8am until 6:30 then run script 2 for 2 hrs then back to script 1 or another script or off etc using my HAT clock?
Any help would be appreciated and thank you in advance.
-
- Posts: 20
- Joined: Sun Mar 04, 2018 12:09 pm
Re: newby -using time with a python script
Sounds like you need the help of 'cron'.
'cron' is a system utility that can execute commands at scheduled times.
'crontab' is the program you use to define 'cron' tasks.
Check the man pages for more details
If you really need to run you scripts as root then use
The comments in the file will describe the format of the entries. You probably want to ad entries like
'cron' is a system utility that can execute commands at scheduled times.
'crontab' is the program you use to define 'cron' tasks.
Check the man pages for more details
Code: Select all
man cron
man crontab
man 5 crontab
Code: Select all
sudo crontab -e
The comments in the file will describe the format of the entries. You probably want to ad entries like
Code: Select all
# Run at 8:00 on weekdays
0 8 * * 1-5 /<path-to>/runtext.py
# Run at 6:30pm on weekdays
30 18 * * 1-5 /<path-to>/runtext1.py
Have Fun ...
-
- Posts: 20
- Joined: Sun Mar 04, 2018 12:09 pm
Re: newby -using time with a python script
Thanks @ Jostle another small question how could I get it to start at say 8am and stop at 6:30 on the same day ? would I just add the cntrl C command (which stops the board) at a certain time ? if so I'm not really sure how to incorporate it ..Really appreciate the help 

Re: newby -using time with a python script
I assume, from your question, that your scripts run continuously in a loop until killed.
If that is the case then I would just add a crontab entry to kill the running script.
So, if your first script is /home/pi/script1.py and you second script is /home/pi/script2.py, your crontab could look like this
Have a look at
for details of the options available for pkill.
If that is the case then I would just add a crontab entry to kill the running script.
So, if your first script is /home/pi/script1.py and you second script is /home/pi/script2.py, your crontab could look like this
Code: Select all
# Start script1.py at 8:00am on weekdays
0 8 * * 1-5 /home/pi/script1.py
# Kill script1.py at 6:30pm on weekdays
30 18 * * 1-5 pkill -f /home/pi/script1.py
# Start script2.py at 6:30pm on weekdays
30 18 * * 1-5 /home/pi/script2.py
# Kill script2.py at 8:30pm on weekdays
30 20 * * 1-5 pkill -f /home/pi/script2.py
Have a look at
Code: Select all
man pkill
Have Fun ...
-
- Posts: 20
- Joined: Sun Mar 04, 2018 12:09 pm
Re: newby -using time with a python script
Thanks Again @Jostle what I actually do is run 1 script ex:=script1.py at 8am during wekkdays and do a (Ctrl + c ) at 6:30 as the Ctrl key +c stops the Leds from scrolling the message and on weekends another script Ex:script2.py with the Ctrl+c key to stop it at a certain time. Sorry If I'm not getting it so in my Kill crontab it would contain the keystrokes (Ctrl+c) or the kill instructions you gave me are understood by Pi as the same ? I normally do this all in the terminal window (which is always up on the PI) so really I just arrow up in the am and hit enter as this brings up the previous command and at 6:30 Ctrl+c to stop (not sure if I'm clear)
Re: newby -using time with a python script
I found the schedule function nice and easy to use. Just include the code in your script. Check out the schedule documentation for other options
In the example below you can add an open and close event line for each day as required. The ShopOpen and ShopClosed functions will run once (1 time) when the appropriate time is reached.
In the example below you can add an open and close event line for each day as required. The ShopOpen and ShopClosed functions will run once (1 time) when the appropriate time is reached.
Code: Select all
import time
import schedule
def ShopOpen():
print "Shop Open"
#Put your sign control for shop open here
def ShopClosed():
print "Shop Closed"
#Put your sign control for shop closed here
schedule.every().thursday.at("09:00").do(ShopOpen)
schedule.every().thursday.at("13:00").do(ShopClosed)
while True:
schedule.run_pending() # checks if the time matches the scheduled event
time.sleep(1)
-
- Posts: 20
- Joined: Sun Mar 04, 2018 12:09 pm
Re: newby -using time with a python script
Thanks again @Jostle for your time and patience I will try that
Re: newby -using time with a python script
Thanks to rkn704 for the info on the schedule function in python.
All cron does is execute the commands in the crontab at the given times, just as if you had entered the same command in a terminal. The difference is that they are run in the background, rather than as a child process of the shell.
When you type (Ctrl + c ) in a terminal the shell sends a kill signal to the child process that is running in the foreground.
'pkill' is just a linux command that sends a kill signal to any process that matches the given command name. So 'pkill' can be used to terminate any process even if it is running in the background, that is, not a child of a shell.
I suggest you try running your script in a terminal and running (based on your original example) from a second terminal to see how it works.
You should be able to get the results you need from 'cron' using the example I gave, with suitable changes to the times and commands.
All cron does is execute the commands in the crontab at the given times, just as if you had entered the same command in a terminal. The difference is that they are run in the background, rather than as a child process of the shell.
When you type (Ctrl + c ) in a terminal the shell sends a kill signal to the child process that is running in the foreground.
'pkill' is just a linux command that sends a kill signal to any process that matches the given command name. So 'pkill' can be used to terminate any process even if it is running in the background, that is, not a child of a shell.
I suggest you try running your script in a terminal and running (based on your original example)
Code: Select all
sudo pkill -f ./runtext.py
You should be able to get the results you need from 'cron' using the example I gave, with suitable changes to the times and commands.
Have Fun ...
-
- Posts: 20
- Joined: Sun Mar 04, 2018 12:09 pm
Re: newby -using time with a python script
Thats right I missed that Thank you rkn704 for that and thank you for your time Jostle
-
- Posts: 20
- Joined: Sun Mar 04, 2018 12:09 pm
Re: newby -using time with a python script
ok new question if anyone can help please do
I have a text script at location> /home/pi/rpi-rgb-led-matrix/bindings/python/samples called open green.py. It runs a scrolling text on a RGB Matrix screen attached to my Pi
I can run it if I cd to that directory and run sudo./opengreen.py. it runs until I press CTRL-C to stop it
I want to use a cronjob to run this every day of the week Tue to Friday from 10:30am to 6:30pm then I want it to stop running and or have the choice to run another script till midnight (same path but called closedor.py).
So basically my display board would run the open green.py from 10:30 till 8:30pm then switch to closed till midnight then shut off by killing the script.
I've tried using sudo crontab -e to write the crontab at root and this is what I put in the file
# Edit this file to introduce tasks to be run by cron.
#
path=/home/pi/rpi-rgb-led-matrix/bindings/python/samples
30 10-20 * * 2-5 /home/pi/rpi-rgb-led-matrix/bindings/python/samples/opengreen.$
#crontab to run open sign
I'm assuming this will run at 10:30 till 8:30 Tuesday till Friday but you know what happens when you assume
But nothing happens ?? I do the reboot and I did the /etc/init.d/cron start it asks me for a password but again nothing
If I run the cron status I get
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-03-12 12:39:07 ADT; 32min ago
Docs: man:cron(8)
Main PID: 304 (cron)
CGroup: /system.slice/cron.service
└─304 /usr/sbin/cron -f
Mar 12 13:04:01 raspberrypi CRON[1251]: pam_unix(cron:session): session clos… pi
Mar 12 13:06:01 raspberrypi CRON[1261]: pam_unix(cron:session): session open…=0)
Mar 12 13:06:01 raspberrypi CRON[1265]: (pi) CMD (/home/pi/CronJobs/RunACron…sh)
Mar 12 13:06:01 raspberrypi CRON[1261]: pam_unix(cron:session): session clos… pi
Mar 12 13:08:01 raspberrypi CRON[1280]: pam_unix(cron:session): session open…=0)
Mar 12 13:08:01 raspberrypi CRON[1284]: (pi) CMD (/home/pi/CronJobs/RunACron…sh)
Mar 12 13:08:01 raspberrypi CRON[1280]: pam_unix(cron:session): session clos… pi
Mar 12 13:10:01 raspberrypi CRON[1309]: pam_unix(cron:session): session open…=0)
Mar 12 13:10:01 raspberrypi CRON[1313]: (pi) CMD (/home/pi/CronJobs/RunACron…sh)
Mar 12 13:10:01 raspberrypi CRON[1309]: pam_unix(cron:session): session clos… pi
Hint: Some lines were ellipsized, use -l to show in full.
I have no idea what I'm doing wrong as I am real green at this any help at all would be appreciated I know I have to add more to get it to stop but I need help in first getting it to work?? Also as a side not Pkill does not seem to stop the script??
I have a text script at location> /home/pi/rpi-rgb-led-matrix/bindings/python/samples called open green.py. It runs a scrolling text on a RGB Matrix screen attached to my Pi
I can run it if I cd to that directory and run sudo./opengreen.py. it runs until I press CTRL-C to stop it
I want to use a cronjob to run this every day of the week Tue to Friday from 10:30am to 6:30pm then I want it to stop running and or have the choice to run another script till midnight (same path but called closedor.py).
So basically my display board would run the open green.py from 10:30 till 8:30pm then switch to closed till midnight then shut off by killing the script.
I've tried using sudo crontab -e to write the crontab at root and this is what I put in the file
# Edit this file to introduce tasks to be run by cron.
#
path=/home/pi/rpi-rgb-led-matrix/bindings/python/samples
30 10-20 * * 2-5 /home/pi/rpi-rgb-led-matrix/bindings/python/samples/opengreen.$
#crontab to run open sign
I'm assuming this will run at 10:30 till 8:30 Tuesday till Friday but you know what happens when you assume

But nothing happens ?? I do the reboot and I did the /etc/init.d/cron start it asks me for a password but again nothing
If I run the cron status I get
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-03-12 12:39:07 ADT; 32min ago
Docs: man:cron(8)
Main PID: 304 (cron)
CGroup: /system.slice/cron.service
└─304 /usr/sbin/cron -f
Mar 12 13:04:01 raspberrypi CRON[1251]: pam_unix(cron:session): session clos… pi
Mar 12 13:06:01 raspberrypi CRON[1261]: pam_unix(cron:session): session open…=0)
Mar 12 13:06:01 raspberrypi CRON[1265]: (pi) CMD (/home/pi/CronJobs/RunACron…sh)
Mar 12 13:06:01 raspberrypi CRON[1261]: pam_unix(cron:session): session clos… pi
Mar 12 13:08:01 raspberrypi CRON[1280]: pam_unix(cron:session): session open…=0)
Mar 12 13:08:01 raspberrypi CRON[1284]: (pi) CMD (/home/pi/CronJobs/RunACron…sh)
Mar 12 13:08:01 raspberrypi CRON[1280]: pam_unix(cron:session): session clos… pi
Mar 12 13:10:01 raspberrypi CRON[1309]: pam_unix(cron:session): session open…=0)
Mar 12 13:10:01 raspberrypi CRON[1313]: (pi) CMD (/home/pi/CronJobs/RunACron…sh)
Mar 12 13:10:01 raspberrypi CRON[1309]: pam_unix(cron:session): session clos… pi
Hint: Some lines were ellipsized, use -l to show in full.
I have no idea what I'm doing wrong as I am real green at this any help at all would be appreciated I know I have to add more to get it to stop but I need help in first getting it to work?? Also as a side not Pkill does not seem to stop the script??