Jump to content

Recommended Posts

Posted

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

Posted

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).

Posted

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

Posted (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 by jinnantonnixx
Posted

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

Posted

 

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.

Posted
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()

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...