I think what I want is straight forward.
Python script to restart my Raspberry Pi after 23 hours and 59 minutes. The reason I am trying to do this, instead of set times with a cron job, is the Pi has no onboard battery for a clock so I don't care what the time is (if connected to internet, it will source current time), just a count down of 23 hours and 59 minutes from the script starting.
This is as far as I have got;
def restart():
SendEmail = SendEmail "SYSTEM RESTART", "ncam.py auto restart initiated" msg['Subject'], body)
command = "/usr/bin/sudo /sbin/shutdown -r now"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
output = process.communicate()[0]
Also I want to send an email to myself with the set parameters as above.
You are going to want some variant of this:
import time
import os
currentTime = time.time()
tomorrow = currentTime + 86340000
while time.time() < tomorrow:
do.yourCode()
os.system("poweroff")
Put something like that in your function and it will do what you want.
You should probably change to
while time.time() < tomorrow
to avoid any potential "miss" of the exact millisecond to match.
You can simply restart raspberry with 'sudo reboot' command.
Just put this command inside a python code and run it as system command. For example this code count down from 1 to 10 before restart:
import time
import os
for i in range(1,10):
print 'hello',i
#Do your code here
time.sleep(1)
os.system("sudo reboot")
Use this method to countdown whatever time and restart pi.
Related
I know there is already the same question as this, but I'm still gonna post this.
I'm trying to display some text to my I2c Lcd using my raspberry pi, I installed the driver from Github using gitclone "git clone https://github.com/the-raspberry-pi-guy/lcd.git) but the problem is every time I run the demo code which is this:
import drivers
from time import sleep
# Load the driver and set it to "display"
# If you use something from the driver library use the "display." prefix first
display = drivers.Lcd()
# Main body of code
try:
while True:
# Remember that your sentences can only be 16 characters long!
print("Writing to display")
display.lcd_display_string("Greetings Human!", 1) # Write line of text to first line of display
display.lcd_display_string("Demo Pi Guy code", 2) # Write line of text to second line of display
sleep(2) # Give time for the message to be read
display.lcd_display_string("I am a display!", 1) # Refresh the first line of display with a different message
sleep(2) # Give time for the message to be read
display.lcd_clear() # Clear the display of any data
sleep(2) # Give time for the message to be read
except KeyboardInterrupt:
# If there is a KeyboardInterrupt (when you press ctrl+c), exit the program and cleanup
print("Cleaning up!")
display.lcd_clear()
I keep getting the IOError: [Errno 121] Remote I/O error, i've already tried every fix I find from the internet, starting from enabling the i2c in the raspi-config, checking the connection, reinstalling drivers and changing i2c LCD etc. but the error still keep from coming up, please help me with this one I don't know how to fix this.
Getting started with MicroPython and having problems with classes in separate files:
In main.py:
import clientBase
import time
if __name__ == "__main__":
time.sleep(15) # Delay to open Putty
print("Starting")
print("Going to class")
cb = clientBase.ClientBaseClass
cb.process()
In clientBase.py:
class ClientBaseClass:
def __init__(self):
print("init")
def process(self):
print("Process")
Compiles and copies to Pico without errors but does not run. Putty output: No idea how to run Putty (or other port monitor) without blocking port!
MPY: soft reboot
Traceback (most recent call last):
Thanks
Python Conslole:
"C:\Users\jluca\OneDrive\Apps\Analytical Engine\Python\Client\venv\Scripts\python.exe" "C:\Program Files\JetBrains\PyCharm Community Edition 2021.2.4\plugins\python-ce\helpers\pydev\pydevconsole.py" --mode=client --port=59708
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['C:\Users\jluca\OneDrive\Apps\Analytical Engine\Python\Client', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\stdlib', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\micropython', 'C:\Users\jluca\AppData\Roaming\JetBrains\PyCharmCE2021.2\plugins\intellij-micropython\typehints\rpi_pico', 'C:/Users/jluca/OneDrive/Apps/Analytical Engine/Python/Client'])
PyDev console: starting.
Python 3.10.3 (tags/v3.10.3:a342a49, Mar 16 2022, 13:07:40) [MSC v.1929 64 bit (AMD64)] on win32
The first problem I see here is that you're not properly instantiating the ClientBaseClass object. You're missing parentheses here:
if __name__ == "__main__":
time.sleep(15) # Delay to open Putty
print("Starting")
print("Going to class")
cb = clientBase.ClientBaseClass # <-- THIS IS INCORRECT
cb.process()
This is setting the variable cb the class ClientBaseClass, rather than creating a new object of that class.
You need:
if __name__ == "__main__":
time.sleep(15) # Delay to open Putty
print("Starting")
print("Going to class")
cb = clientBase.ClientBaseClass()
cb.process()
I don't know if that's your only problem or not; seeing your traceback will shed more details on the problem.
If I fix that one problem, it all seems to work. I'm using ampy to transfer files to my Pico board (I've also repeated the same process using the Thonny edit, which provides a menu-driven interface for working with Micropython boards):
$ ampy -p /dev/usbserial/3/1.4.2 put main.py
$ ampy -p /dev/usbserial/3/1.4.2 put clientBase.py
$ picocom -b 115200 /dev/usbserial/3/1.4.2
I press return to get the Micropython REPL prompt:
<CR>
>>>
And then type CTRL-D to reset the board:
>>> <CTRL-D>
MPY: soft reboot
And then the board comes up, the code executes as expected:
<pause for 15 seconds>
Starting
Going to class
init
Process
MicroPython v1.18 on 2022-01-17; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>>
(note that if you replace MicroPython with CircuitPython,the Pico will show up as a drive and you can just drag-and-drop files on it.)
Tried micropython and circuitpython with Pycharm, Thonny and VisualStudio code. The only thing that reliably works is CircuitPython with Mu editor. I think its all about the way the .py files are copied to the Pico board and life's too short to do more diagnostics. Mu is pretty basic but it works! Thanks for the help.
I have simple Locust script with one task with http request.
I want to run this task 100 times on 10 users and than stop run script.
Is there any simple way to do it. I know --run-time parameter but it only stop after the specified amount of time
Below my script:
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
wait_time = between(1, 2)
host = "https://allegro.pl"
#task(1)
def getHome(self):
self.client.get("/dzial/dom-i-ogrod", name = "Get Home and Garden")
Another option, provided by locust-plugins, is the -i parameter: https://github.com/SvenskaSpel/locust-plugins#command-line-options
It should be a little more reliable, as it explicitly calls runner.quit()
If you are not running distributed you can have a global counter and increment it in the task and once it reaches the desired count you can stop the runner, like :
from locust import HttpUser, task, between
counter=0
class QuickstartUser(HttpUser):
wait_time = between(1, 2)
host = "https://allegro.pl"
#task(1)
def getHome(self):
if counter == 100:
self.environment.runner.stop()
self.client.get("/dzial/dom-i-ogrod", name = "Get Home and Garden")
counter = counter + 1`
and if you are running distributed it is best you use some external counter to keep track of requests like redis or something like that.
I try to add a cron job on an elearning.mysite.gr(Moodle). Although my host gives this message every hour.
Oct 2 1:10:01 linux CROND[123456]: (admin) CMD (touch /tmp/test.txt > /dev/null)
On my site administration -> notifications get this message:
The admin/cli/cron.php script has not been run for 3 days 2 hours and should run every 1 min.
At moodle documentation
/path/to/moodle/admin/cli/cron.php, can i use it ? in which way ?
I tried this :
/usr/bin/php /path/to/moodle/admin/cli/cron.php
but gives me that the process completed with error at one minute
only this touch /tmp/test.txt > /dev/null
completed with success.
Moodle Documentation:
The CLI (command line interpreter) script. This will be at the path /path/to/moodle/admin/cli/cron.php
If in doubt, this is the correct script to use. This needs to be run by a 'PHP CLI' program on your computer. So the final command may look something like /usr/bin/php /path/to/moodle/admin/cli/cron.php You can (and should) try this on your command line to see if it works. WARNING: Check your command-line PHP version is compatible with your chosen version of Moodle.-How to check it?
The command-line PHP program is different to the one running your web site and is not always the same version.
I don't know what to do...I will appreciate any help!!!
I tried by my host panel interface :
Type of progress:
Command line
URL
PHP
i should choose one from 3.
Command*:
placeholder to add my command to be excecuted
Excecute:
Dropdown
1.Cron style
2.Daily at 00:00
I use command line.
touch /tmp/test.txt
When i add
cron style and 1 * * * * * , get message for syntax.
and for
daily at 00:00 success message but on my notification of my site has
The admin/cli/cron.php script has not been run for 17 days 22 hours and should run every 1 min.
That's the icon of my Plesk Login. To change the path/to/moodle/admin/cli/cron.php xould i check the file manager in order to find cron.php file ??
As you add the cron from a web interface (maybe Plesk ?) you only need this part:
/path/to/moodle/admin/cli/cron.php
replace /path/to/moodle/ with the real path on your server, usulay something like /var/www/moodle/ or /var/www/vhosts/domain.name/httpdocs/moodle
To run every 1 minute cron style is :
* * * * *
If you can choose PHP version, choose the same version as the one you are using for Moodle.
Finally, I give this touch /tmp/test.txt ->and at cron style the touch /tmp/test.txt, as you suggest to me.
Because the other commands cannot complete with success.
But in my notifications,(Site administration-Moodle) i still get the message that cron don't run.
My Operating System is Manjora17.1.12, the Python version is 3.7.0, and the Supervisor's version is 3.3.4.
I have a python script, it just shows a notification. The code is:
import os
os.system('notify-send hello')
The supervisor config is :
[program:test_notify]
directory=/home/zz
command=python -u test_notify.py
stdout_logfile = /home/zz/supervisord.d/log/test_notify.log
stderr_logfile = /home/zz/supervisord.d/log/test_notify.log
But when I execute the python script with the supervisor, it doesn't show the notification.
Proper environment variables need to be set (DISPLAY & DBUS_SESSION_BUS_ADDRESS). You can do it in many different ways, depending on your needs, like e.g.
a) per subprocess
import os
os.system('DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send hello')
b) in script globally
import os
os.environ['DISPLAY'] = ':0'
os.environ['DBUS_SESSION_BUS_ADDRESS'] = 'unix:path=/run/user/1000/bus'
os.system('notify-send hello')
c) in supervisor config per program
[program:test_notify]
;
; your variables
;
user=john
environment=DISPLAY=":0",DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
The above examples have a couple of assumptions (you may want to change these settings accordingly):
script is run as user john
UID of user john is 1000
notification appear on display :0
To run script as root and show notification for regular user, use sudo as described on Arch wiki Desktop_notifications.