How do I write to logs, close files etc. when my python program gets killed by a system shutdown? The program is a long running process which uses a while True loop i.e.
while True:
# Open files
#do long running process
If the system is shutdown while this is running, can it get a signal so that it knows to cleanup e.g. close files, write "shutting down..." to log etc. What would the code look like to handle this?
Code is Python 3 running on Debian Wheezy. Program will be started by an init script on system start up.
Thanks.
Related
I am developing in Python 3.10.0
I need to kill a process while my program is running.
This is my code to close a program:
for process in psutil.process_iter():
if process.name() == 'app.exe':
process.kill()
This works, but if the program name changes, It's will not close anymore.
Is there a way to close a specific program without checking it's name?
I am writing a Python C extension that contains multiple C pthreads. Eventually these threads are sent a SIGTERM in order for them to exit. When I step through the extension in GDB these threads exit successfully, and I return back to the Python interpreter where I can continue to run commands. It is also working successfully in the Python interpreter.
However, when I try to run a Python file that contains similar behavior, the entire program terminates after the signal is sent to the child thread.
I am confused as to how the signal is propagating up from the threads to the program itself, any guidance is appreciated.
When I am away from my mountain, I monitor my Photo-voltaic system with Raspberry Pi and a small python script to read data and send it to my web page every hour. That is launched by a electo-mechanical switch that lights up the stuff for 15 minutes. But during that period, the script may run twice, which I would like to prevent as the result is messy (lsdx.eu/GPV_ZSDX).
I want to add some line at the end of the script to stop it once it has run once and possibly stop raspbian as well for a clean exit before the power is off.
- "exit" only exits a loop but the script is still running
- of course Ctrl+C won't do as I am away;
Could not find any tip in these highly technical messages in StackOverflow or in Rasbian help either.
Any tip?
Thanks
The exit() command should exit the program (break is the statement that will exit a loop). What behavior are you seeing?
To shut down , try :
python3:
from subprocess import run
run('poweroff', shell=True)
python2:
from subprocess import call
call('poweroff')
Note: poweroff may be called shutdown on your system and might require additional command line switches to force a shutdown (if needed).
For your case, structure the python script as a function using the following construct:
def read_data():
data_reading_voodo
return message_to_be_sent
def send_message(msg):
perform_message_sending_voodo
log_message_sending_voodoo_success_or_failure
return None
if __name__ == "__main__":
msg = read_data()
send_message(msg)
Structured like this, the python script should exit after running.
Next create a shell sript like follows (assuming bash and python, but modify according to your usage)
#!/bin/bash
python -m /path/to/your/voodo/script && sudo shutdown -h 6
The sudo shudown -h 6 shuts down the raspberrypi 6 minutes after the script is run. This option helps so you have some time after startup to remove the sript if you ever want to stop the run-restart cycle.
Make the shell script executable: chmod 755 run_py_script_then_set_shutdown see man chmod for details
Now create a cronjob to run run_py_script_then_set_shutdown on startup.
crontab -e Then add the following line to your crontab
#reboot /path/to/your/shell/script
Save, reboot the pi, and you're done.
Every time the rpi starts up, the python script should run and exit. Then the rpi will shutdown 6 minutes after the python script exits.
You can (should) adjust the 6 minutes for your purposes.
Thanks for all these answers that help me learn Pyth and Deb.
I finally opted for a very simple solution at the end of the script:
import os
os.system('sudo shutdown now')
But I keep in mind these other solutions
Thanks again,
Lionel
I am writing a script in python and I would like to stop the scipt in the middle to see the objects and things. I used sys.exit() in pyscripter and everything is fine, but when I switch to spyder, the python interpreter is killed and I cannot read anything after the script is stopped.
How to fix it?
I have a python script which in turn executes other python scripts.
I put this as a task on WinXP task scheduler. the thing runs - command prompt is opened, sparks are flying, magic happens... eventually the task is completed, I get a nice
'print script ended!!'
and back to prompt. but Task Scheduler thinks the task is still running ! which in turn prevents it from running it again on daily basis.
so I tried making a BAT file which just calls the script:
script.py
echo pyfinished
to my surprise cannot see 'pyfinished' at the end ...
I have this problem as well. What I did to make sure the script stops is configure the task to stop after 1 hour (or however long the script(s) should take). This kills the task and thus when the task schedule comes around again, it has no problem kicking off.
As for why Task Scheduler can't detect the script is finished, I have no idea. It's royally annoying.
a line with os.system('cmd /K script.py') makes the process stay alive until I manually kill it.