Python Forum
Using a button to kill and restart a script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a button to kill and restart a script
#1
I have a program that monitors a scale and uses LEDs to indicate the weight of the object on the scale. At the start of the script, it gets all the prerequisites and establishes a tare. I then get a print("The scale is ready...") and a quick flash of an LED. I want to run this headless, so that I can rely only on the quick flash of the LED to tell me it is ready. Without the GUI, I have no way of stopping the program if it is already running, then restarting it.

So what I want is to have a program run on boot, waiting for the button input. Using gpiozero, I would like to have the button kill this scale program (if it is running) and restart it, or simply start it if it isn't running. In the event of a power failure or other reboot event, I would simply observe that the LEDs are not on, then I could just remove the item from the scale, press the button, then put it back on. Then the LEDs would indicate the proper level.

Sounds simple to me, just don't know where to start.
Reply
#2
I assume this is on a raspberry pi? If so, I'd probably just find a quick systemd script to start the scale program at multi-user.

You could write another program (also started at multi-user) that just monitors the button. If the button is pushed, it could systemctl restart <other-unit> to start/restart that program.
Reply
#3
https://docs.python.org/3/library/os.html#os.getpid
https://docs.python.org/3/library/os.html#os.kill
https://docs.python.org/3/library/signal...nal.SIGINT

I would have the script you want to restart use os.getpid() to find it's process id, then write the id out to a file as soon as it starts up. The other script, which restarts it, can then just read that file to find the pid, and call os.kill(other_pid, signal.SIGINT) to kill it. Restarting it should be easy, something like os.system("python scale_weight.py &").
Reply
#4
Yes this is on a pi.

Could I kill it by name instead of pid?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  kill python execution program lebossejames 0 235 Mar-16-2024, 11:16 AM
Last Post: lebossejames
  Button to stop while loop from another script Absolutewind 5 881 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 3,520 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  How a Mac OS software can restart itself with admin permission in Python 3.7? Formationgrowthhacking 0 1,768 Sep-03-2020, 05:29 PM
Last Post: Formationgrowthhacking
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,875 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  Hotkey to restart code? DannyB 1 2,747 May-20-2020, 02:52 AM
Last Post: michael1789
  how to check for thread kill flag nanok66 1 2,168 May-09-2020, 10:06 PM
Last Post: nanok66
  Kernel needs to restart ErnestTBass 0 2,346 May-06-2020, 08:37 PM
Last Post: ErnestTBass
  kill thread or process asap, even during time.sleep nanok66 4 2,929 Apr-29-2020, 10:13 AM
Last Post: nanok66
  How to restart Python after input change ozstar 3 2,371 Apr-29-2020, 03:16 AM
Last Post: ozstar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020