SimpleSi Posted August 2, 2012 Posted August 2, 2012 I've created a script #!/bin/bash sudo python scratch_gpio_handler.py that runs a python program to handle traffic between Scratch and the GPIO pins on a RaspberryPi. This program consumes a lot of CPU (as its just sits in a loop awaiting changes) and I'd like users (e.g pupils) to kill it when they are not using it. Using ps aux I find I've got 3 processes running /bin/bash ./sgh (the name of the script file) sudo python scratch_gpio_handler.py python scratch_gpio_handler.py I'm finding that if a I find out the pid of the last one and kill it - the other two seem to be killed as well But I can't work out the syntax to script killing it I can use pkill python but don't want to assume that my script is the only python one running. Any ideas please? regards Simon
pcstru Posted August 2, 2012 Posted August 2, 2012 I think the ultimate is : kill -9 the ps command will as standard only give you your processes and permissions should stop students killing other peoples processes (if that is what you are worried about). With tight loops that idle for long periods, consider "sleep(t)" which will yeild to the thread scheduler. You should find though that you are only using a lot of CPU because that is available to you (i.e, there is nothing else happening on the system).
jinnantonnixx Posted August 2, 2012 Posted August 2, 2012 (edited) You can kill by name, rather than pid, with: killall -v /full_path_to_your_process Edited August 2, 2012 by jinnantonnixx
tom_newton Posted August 2, 2012 Posted August 2, 2012 killall is probably your best method. Ideally though, you need to change your script so it doesn't busyloop...
SimpleSi Posted August 2, 2012 Author Posted August 2, 2012 Sorry - I'm not making myself understood - I know what needs doing - I just don't know the syntax to do it what syntax/script would kill the running process that shows up as python scratch_gpio_handler.py in the command column? Ideally though, you need to change your script so it doesn't busyloop... Well, its a python prog that sits waiting for data to appear on 127.0.0.1:42001 and also monitors changes to GPIO pin inputs so its a bit hard to make it not loop Under normal circumstances (e.g running under windows on a 1.6Ghz machine) then the running python prog wouldn't slow down the machine enough to be an issue but I'm finding that its dragging down the Scratch GUI speed to a point where kids would think it unusable so I'm wanting (as current workaround until we can get Scratch directly talking to the hardware) to just have 2 desktop icons - one to start the script and 1 to kill it. I'm also working on the python prog terminating on receiving a defined Scratch broadcast but since I know <0.1% Python ATM, I'm having difficulty in closing threads gracefully within the program - but I'll get their eventually. I just need this bodge as I'm demo-ing my efforts so far at Monday's RaspberryJam at Preston So back to the question sudo killall ......... (replace the dots please ) Si
jinnantonnixx Posted August 2, 2012 Posted August 2, 2012 (edited) I might be being dense here, but wouldn't that be killall python\ scratch_gpio_handler.py OK, I've never been a fan of killing processes, so could you start it with a nice value? (e.g. nice -n 10 python scratch_gpio_handler.py) or maybe a 'renice' to change the running priority? If you choose a high nice value it drops down in the scheduler priority, perhaps your system won't choke up. For your original problem, I've done something like this to kill specific running stuff, but my library files are at work so I can look tomorrow. Edited August 2, 2012 by jinnantonnixx
tom_newton Posted August 2, 2012 Posted August 2, 2012 Or have the python daemon write a pidfile out... then kill `cat /path/to/pidfile`
tom_newton Posted August 2, 2012 Posted August 2, 2012 Read this, and have fun: select – Wait for I/O Efficiently - Python Module of the Week 1
SimpleSi Posted August 2, 2012 Author Posted August 2, 2012 sudo killall python\ scratch_gpio_handler.py[code] gives [code]python scratch_gpio_handler.py: no process found I've now got this code from another forum sudo ps aux | grep "python scratch_gpio_handler.py" | grep -v grep | awk '{print $2}' | xargs sudo kill -9 which seems to do the job (its gives an error if no scratch_gpio_handler.py actaully running but I can live with that ) Or have the python daemon write a pidfile out... then kill `cat /path/to/pidfile` You DID read my bit about understanding python <0.001%? Simon
dhicks Posted August 3, 2012 Posted August 3, 2012 Read this, and have fun: select – Wait for I/O Efficiently - Python Module of the Week But, if I understand correctly, the problem is with this part (from the link above): Python’s select() function is a direct interface to the underlying operating system implementation. It monitors sockets, open files, and pipes (anything with a fileno() method that returns a valid file descriptor) The assorted Raspberry Pi Linux distributions don't, by default, provide a socket for the GPIO ports. I think there are kernel patches available, but for which version of which kernel is tricky to work out.
dhicks Posted August 3, 2012 Posted August 3, 2012 You DID read my bit about understanding python <0.001%? os.getpid() should do it: import os pidFile = open("/var/tmp/gpio.pid", "w") pidFile.write(os.getpid()) pidFile.close()
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now